home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvicopy / dvicopy.web (.txt) < prev    next >
Texinfo Document  |  1990-10-01  |  195KB  |  4,064 lines

  1. % This is DVICOPY.WEB in text format, as of August 6, 1990.
  2. % This program by P. Breitenlohner is not copyrighted and can be used freely.
  3. % Version 0.9 was finished May 21, 1990.
  4. % Version 0.91 fixed several bugs (May 22, 1990).
  5. % Version 0.92 introduced statistics (May 25, 1990).
  6. % Version 0.95 modified preamble comment, "history" (July 23, 1990).
  7. % Version 1.0 pixel rounding for real devices (August 6, 1990).
  8. % Here is TeX material that gets inserted after \input webmac
  9. \def\hang{\hangindent 3em\indent\ignorespaces}
  10. \font\ninerm=cmr9
  11. \let\mc=\ninerm % medium caps for names like SAIL
  12. \def\PASCAL{Pascal}
  13. \mathchardef\RA="3221 % right arrow
  14. \def\(#1){} % this is used to make section names sort themselves better
  15. \def\9#1{} % this is used for sort keys in the index
  16. \def\title{DVI\lowercase{copy}}
  17. \def\contentspagenumber{1}
  18. \def\topofcontents{\null
  19.   \def\titlepage{F} % include headline on the contents page
  20.   \def\rheader{\mainfont\hfil \contentspagenumber}
  21.   \vfill
  22.   \centerline{\titlefont The {\ttitlefont DVIcopy} processor}
  23.   \vskip 15pt
  24.   \centerline{(Version 1.0, August 1990)}
  25.   \vfill}
  26. \def\botofcontents{\vfill
  27.   \centerline{\hsize 5in\baselineskip9pt
  28.     \vbox{\ninerm\noindent
  29.     This program was developed at the
  30.     Max-Planck-Instiut f\"ur Physik
  31.     (Werner-Heisenberg-Institut), Munich, Germany.
  32.     `\TeX' is a trademark of the American Mathematical Society.}}}
  33. \pageno=\contentspagenumber \advance\pageno by 1
  34. @* Introduction.
  35. The \.{DVIcopy} utility program copies (selected pages of) binary
  36. device-independent (``\.{DVI}'') files that are produced by document
  37. compilers such as \TeX, and replaces all references to characters from
  38. virtual fonts by the typesetting instructions specified for them in
  39. binary virtual-font (``\.{VF}'') files.
  40. This program has two chief purposes: (1)~It can be used as preprocessor
  41. for existing \.{DVI}-related software in cases where this software is
  42. unable to handle virtual fonts or (given suitable \.{VF} files) where
  43. this software cannot handle fonts with more than 128~characters;
  44. and (2)~it serves as an example of a program that reads \.{DVI} and
  45. \.{VF} files correctly, for system programmers who are developing
  46. \.{DVI}-related software.
  47. Goal number (1) is important since quite a few existing programs have
  48. to be adapted to the extened capabilities of Version~3 of \TeX\ which
  49. will require some time. Moreover some existing programs are `as is' and
  50. the source code is, unfortunately, not available.
  51. Goal number (2) needs perhaps a bit more explanation. Programs for
  52. typesetting need to be especially careful about how they do arithmetic; if
  53. rounding errors accumulate, margins won't be straight, vertical rules
  54. won't line up, and so on (see the documentaion of \.{DVItype} for more
  55. details). This program is written as if it were a \.{DVI}-driver for a
  56. hypothetical typesetting device |out_file|, the output file receiving
  57. the copy of the input |dvi_file|. In addition all code related to
  58. |out_file| is concentrated in a few chapters of this program and quite
  59. independent of the rest of the code concerned with the decoding of
  60. \.{DVI} and \.{VF} files and with font substitutions. Thus it should be
  61. relatively easy to replace the device dependent code of this program by
  62. the corresponding code required for a real typesetting device.
  63. Having this in mind \.{DVItype}'s pixel rounding algorithms are included
  64. as conditional code not used by \.{DVIcopy}.
  65. The |banner| and |preamble_comment| strings defined here should be
  66. changed whenever \.{DVIcopy} gets modified.
  67. @d banner=='This is DVIcopy, Version 1.0' {printed when the program starts}
  68. @d preamble_comment=='DVIcopy 1.0 output from '
  69. @d comm_length=24 {length of |preamble_comment|}
  70. @d from_length=6 {length of its |' from '| part}
  71. @ This program is written in standard \PASCAL, except where it is necessary
  72. to use extensions; for example, \.{DVIcopy} must read files whose names
  73. are dynamically specified, and that would be impossible in pure \PASCAL.
  74. All places where nonstandard constructions are used have been listed in
  75. the index under ``system dependencies.''
  76. @!@^system dependencies@>
  77. One of the extensions to standard \PASCAL\ that we shall deal with is the
  78. ability to move to a random place in a binary file; another is to
  79. determine the length of a binary file. Such extensions are not necessary
  80. for reading \.{DVI} files; since \.{DVIcopy} is (a model for) a
  81. production program it should, however, be made as efficient as possible
  82. for a particular system. If \.{DVIcopy} is being used with
  83. \PASCAL s for which random file positioning is not efficiently available,
  84. the following definition should be changed from |true| to |false|; in such
  85. cases, \.{DVIcopy} will not include the optional feature that reads the
  86. postamble first.
  87. @d random_reading==true {should we skip around in the file?}
  88. @ The program begins with a fairly normal header, made up of pieces that
  89. @^system dependencies@>
  90. will mostly be filled in later. The \.{DVI} input comes from file
  91. |dvi_file|, the \.{DVI} output goes to file |out_file|, and messages
  92. go to \PASCAL's standard |output| file.
  93. The \.{TFM} and \.{VF} files are defined later since their external
  94. names are determined dynamically.
  95. If it is necessary to abort the job because of a fatal error, the program
  96. calls the `|jump_out|' procedure, which goes to the label |final_end|.
  97. @d final_end = 9999 {go here to wrap it up}
  98. @p @t\4@>@<Compiler directives@>@/
  99. program DVI_copy(@!dvi_file,@!out_file,@!output);
  100. label final_end;
  101. const @<Constants in the outer block@>@/
  102. type @<Types in the outer block@>@/
  103. var @<Globals in the outer block@>@/
  104. @<Error handling procedures@>@/
  105. procedure initialize; {this procedure gets things started properly}
  106.   var @<Local variables for initialization@>@/
  107.   begin print_ln(banner);@/
  108.   @<Set initial values@>@/
  109.   end;
  110. @ On some systems it is necessary to use various integer subrange types
  111. in order to make \.{\title} efficient; this is true in particular for
  112. frequently used variables such as loop indices. Consider an integer
  113. variable |x| with values in the range |0..255|: on most small systems
  114. |x| should be a one or two byte integer whereas on most large systems
  115. |x| should be a four byte integer.
  116. Clearly the author of a program knows best which range of values is
  117. required for each variable; thus \.{\title} never uses \PASCAL's |integer|
  118. type. All integer variables are declared as one of the integer subrange
  119. types defined below as \.{WEB} macros or \PASCAL\ types; these definitions
  120. can be used without system-dependent changes, provided the signed 32~bit
  121. integers are a subset of the standard type |integer|, and the compiler
  122. automatically uses the optimal representation for integer subranges
  123. (both conditions need not be satisfied for a particular system).
  124. @^system dependencies@>
  125. The complementary problem of storing large arrays of integer type
  126. variables as compactly as possible is addressed differently; here
  127. \.{\title} uses a \PASCAL\ |type|~declaration for each kind of array
  128. element.
  129. Note that the primary purpose of these definitions is optimizations, not
  130. range checking. All places where optimization for a particular system is
  131. highly desirable have been listed in the index under ``optimization.''
  132. @!@^optimization@>
  133. @d int_32 == integer {signed 32~bit integers}
  134. @<Types...@>=
  135. @!int_31 = 0..@"7FFFFFFF; {unsigned 31~bit integer}
  136. @!int_24u = 0..@"FFFFFF; {unsigned 24~bit integer}
  137. @!int_24 = -@"800000..@"7FFFFF; {signed 24~bit integer}
  138. @!int_23 = 0..@"7FFFFF; {unsigned 23~bit integer}
  139. @!int_16u = 0..@"FFFF; {unsigned 16~bit integer}
  140. @!int_16 = -@"8000..@"7FFF; {signed 16~bit integer}
  141. @!int_15 = 0..@"7FFF; {unsigned 15~bit integer}
  142. @!int_8u = 0..@"FF; {unsigned 8~bit integer}
  143. @!int_8 = -@"80..@"7F; {signed 8~bit integer}
  144. @!int_7 = 0..@"7F; {unsigned 7~bit integer}
  145. @ Some of this code is optional for use when debugging only;
  146. such material is enclosed between the delimiters |debug| and $|gubed|$.
  147. Other parts, delimited by |stat| and $|tats|$, are optionally included
  148. if statistics about \.{\title}'s memory usage are desired.
  149. @d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
  150. @d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
  151. @f debug==begin
  152. @f gubed==end
  153. @d stat==@{ {change this to `$\\{stat}\equiv\null$'
  154.   when gathering usage statistics}
  155. @d tats==@t@>@} {change this to `$\\{tats}\equiv\null$'
  156.   when gathering usage statistics}
  157. @f stat==begin
  158. @f tats==end
  159. @ As mentioned above, \.{DVIcopy} has two chief purposes: (1)~It produces
  160. a copy of the input \.{DVI} file with all references to characters from
  161. virtual fonts replaced by their expansion as specified in the character
  162. packets of \.{VF} files; and (2)~it serves as an example of a program
  163. that reads \.{DVI} and \.{VF} files correctly, for system programmers
  164. who are developing \.{DVI}-related software.
  165. Parts of the program that are needed in (2) but not in (1) are delimited
  166. by the codewords `$|device|\ldots|ecived|$'; these are mostly the pixel
  167. rounding algorithms used to convert the \.{DVI} units of a \.{DVI} file
  168. to the raster units of a real output device and have been copied more or
  169. less verbatim from \.{DVItype}.
  170. @d device==@{ {change this to `$\\{device}\equiv\null$' when output
  171.   for a real device is produced}
  172. @d ecived==@t@>@} {change this to `$\\{ecived}\equiv\null$' when output
  173.   for a real device is produced}
  174. @f device==begin
  175. @f ecived==end
  176. @ The \PASCAL\ compiler used to develop this program has ``compiler
  177. directives'' that can appear in comments whose first character is a dollar sign.
  178. In production versions of \.{\title} these directives tell the compiler that
  179. @^system dependencies@>
  180. it is safe to avoid range checks and to leave out the extra code it inserts
  181. for the \PASCAL\ debugger's benefit, although interrupts will occur if
  182. there is arithmetic overflow.
  183. @<Compiler directives@>=
  184. @{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
  185. @!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
  186. @ Labels are given symbolic names by the following definitions. We insert
  187. the label `|exit|:' just before the `\ignorespaces|end|\unskip' of a
  188. procedure in which we have used the `|return|' statement defined below;
  189. the label `|restart|' is occasionally used at the very beginning of a
  190. procedure; and the label `|reswitch|' is occasionally used just prior to
  191. a \&{case} statement in which some cases change the conditions and we wish to
  192. branch to the newly applicable case.
  193. Loops that are set up with the \&{loop} construction defined below are
  194. commonly exited by going to `|done|' or to `|found|' or to `|not_found|',
  195. and they are sometimes repeated by going to `|continue|'.
  196. @d exit=10 {go here to leave a procedure}
  197. @d restart=20 {go here to start a procedure again}
  198. @d reswitch=21 {go here to start a case statement again}
  199. @d continue=22 {go here to resume a loop}
  200. @d done=30 {go here to exit a loop}
  201. @d found=31 {go here when you've found it}
  202. @d not_found=32 {go here when you've found something else}
  203. @ The term |print| is used instead of |write| when this program writes on
  204. |output|, so that all such output could easily be redirected if desired;
  205. the term |d_print| is used for conditional output if we are debugging.
  206. @d print(#)==write(output,#)
  207. @d print_ln(#)==write_ln(output,#)
  208. @d d_print(#)==@!debug print(#) @; @+ gubed
  209. @d d_print_ln(#)==@! debug print_ln(#) @; @+ gubed
  210. @ Here are some macros for common programming idioms.
  211. @d incr(#) == #:=#+1 {increase a variable by unity}
  212. @d decr(#) == #:=#-1 {decrease a variable by unity}
  213. @d Incr_Decr(#) == #
  214. @d Incr(#) == #:=#+Incr_Decr {increase a variable}
  215. @d Decr(#) == #:=#-Incr_Decr {decrease a variable}
  216. @d loop == @+ while true do@+ {repeat over and over until a |goto| happens}
  217. @d do_nothing == {empty statement}
  218. @d return == goto exit {terminate a procedure call}
  219. @f return == nil
  220. @f loop == xclause
  221. @ We assume that |case| statements may include a default case that applies
  222. if no matching label is found. Thus, we shall use constructions like
  223. @^system dependencies@>
  224. $$\vbox{\halign{#\hfil\cr
  225. |case x of|\cr
  226. 1: $\langle\,$code for $x=1\,\rangle$;\cr
  227. 3: $\langle\,$code for $x=3\,\rangle$;\cr
  228. |othercases| $\langle\,$code for |x<>1| and |x<>3|$\,\rangle$\cr
  229. |endcases|\cr}}$$
  230. since most \PASCAL\ compilers have plugged this hole in the language by
  231. incorporating some sort of default mechanism. For example, the compiler
  232. used to develop \.{WEB} and \TeX\ allows `|others|:' as a default label,
  233. and other \PASCAL s allow syntaxes like `\ignorespaces|else|\unskip' or
  234. `\&{otherwise}' or `\\{otherwise}:', etc. The definitions of |othercases|
  235. and |endcases| should be changed to agree with local conventions. (Of
  236. course, if no default mechanism is available, the |case| statements of
  237. this program must be extended by listing all remaining cases.
  238. Donald~E. Knuth, the author of the \.{WEB} system program \.{TANGLE},
  239. @^Knuth, Donald Ervin@>
  240. would have taken the trouble to modify \.{TANGLE} so that such extensions
  241. were done automatically, if he had not wanted to encourage \PASCAL\
  242. compiler writers to make this important change in \PASCAL, where it belongs.)
  243. @d othercases == others: {default for cases not listed explicitly}
  244. @d endcases == @+end {follows the default case in an extended |case| statement}
  245. @f othercases == else
  246. @f endcases == end
  247. @ The definition of |max_font_type| should be adapted to the number of
  248. font types used by the program; the first two values have a fixed meaning:
  249. |new_font_type=0| indicates that a font has been defined but has
  250. not yet been used, and |vf_font_type=1| indicates a virtual font;
  251. font type values |>=2| indicate real fonts and different font types
  252. could be used to distinguish various kinds of font files (\.{GF} or
  253. \.{PK} or \.{PXL}).
  254. @!@^font types@>
  255. @d new_font_type=0 {this font has been defined but has not yet been used}
  256. @d vf_font_type=1 {this font is a virtual font}
  257. @d out_font_type=2 {this font has been used in |out_file|}
  258. @d max_font_type=2
  259. @ The following parameters can be changed at compile time to extend or
  260. reduce \.{DVIcopy}'s capacity.
  261. @<Constants...@>=
  262. @!max_fonts=100; {maximum number of distinct fonts}
  263. @!max_chars=10000; {maximum number of different characters among all fonts}
  264. @!max_widths=3000; {maximum number of different characters widths}
  265. @!max_packets=5000; {maximum number of different characters packets;
  266.   must be less than 65536}
  267. @!max_bytes=30000; {maximum number of bytes for characters packets}
  268. @!max_recursion=10; {\.{VF} files shouldn't recurse beyond this level}
  269. @!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth}
  270. @!name_length=50; {a file name shouldn't be longer than this}
  271. @ A global variable called |history| will contain one of four values
  272. at the end of every run: |spotless| means that no unusual messages were
  273. printed; |harmless_message| means that a message of possible interest
  274. was printed but no serious errors were detected; |error_message| means that
  275. at least one error was found; |fatal_message| means that the program
  276. terminated abnormally. The value of |history| does not influence the
  277. behavior of the program; it is simply computed for the convenience
  278. of systems that might want to use such information.
  279. @d spotless=0 {|history| value for normal jobs}
  280. @d harmless_message=1 {|history| value when non-serious info was printed}
  281. @d error_message=2 {|history| value when an error was noted}
  282. @d fatal_message=3 {|history| value when we had to stop prematurely}
  283. @d mark_harmless==@t@>@+if history=spotless then history:=harmless_message
  284. @d mark_error==history:=error_message
  285. @d mark_fatal==history:=fatal_message
  286. @<Glob...@>=@!history:spotless..fatal_message; {how bad was this run?}
  287. @ @<Set init...@>=history:=spotless;
  288. @* The character set.
  289. Like all programs written with the  \.{WEB} system, \.{\title} can be
  290. used with any character set. But it uses ASCII code internally, because
  291. the programming for portable input-output is easier when a fixed internal
  292. code is used, and because \.{DVI} and \.{VF} files use ASCII code for
  293. file names and certain other strings.
  294. The next few sections of \.{\title} have therefore been copied from the
  295. analogous ones in the \.{WEB} system routines. They have been considerably
  296. simplified, since \.{\title} need not deal with the controversial
  297. ASCII codes less than @'40 or greater than @'176.
  298. If such codes appear in the \.{DVI} file,
  299. they will be printed as question marks.
  300. @<Types...@>=
  301. @!ASCII_code=" ".."~"; {a subrange of the integers}
  302. @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
  303. character sets were common, so it did not make provision for lower case
  304. letters. Nowadays, of course, we need to deal with both upper and lower case
  305. alphabets in a convenient way, especially in a program like \.{\title}.
  306. So we shall assume that the \PASCAL\ system being used for \.{\title}
  307. has a character set containing at least the standard visible characters
  308. of ASCII code (|"!"| through |"~"|).
  309. Some \PASCAL\ compilers use the original name |char| for the data type
  310. associated with the characters in text files, while other \PASCAL s
  311. consider |char| to be a 64-element subrange of a larger data type that has
  312. some other name.  In order to accommodate this difference, we shall use
  313. the name |text_char| to stand for the data type of the characters in the
  314. output file.  We shall also assume that |text_char| consists of
  315. the elements |chr(first_text_char)| through |chr(last_text_char)|,
  316. inclusive. The following definitions should be adjusted if necessary.
  317. @^system dependencies@>
  318. @d text_char == char {the data type of characters in text files}
  319. @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
  320. @d last_text_char=127 {ordinal number of the largest element of |text_char|}
  321. @<Types...@>=
  322. @!text_file=packed file of text_char;
  323. @ @<Local variables for init...@>=
  324. @!i:int_16; {loop index for initializations}
  325. @ The \.{\title} processor converts between ASCII code and
  326. the user's external character set by means of arrays |xord| and |xchr|
  327. that are analogous to \PASCAL's |ord| and |chr| functions.
  328. @<Globals...@>=
  329. @!xord: array [text_char] of ASCII_code;
  330.   {specifies conversion of input characters}
  331. @!xchr: array [0..255] of text_char;
  332.   {specifies conversion of output characters}
  333. @ Under our assumption that the visible characters of standard ASCII are
  334. all present, the following assignment statements initialize the
  335. |xchr| array properly, without needing any system-dependent changes.
  336. @<Set init...@>=
  337. for i:=0 to @'37 do xchr[i]:='?';
  338. xchr[@'40]:=' ';
  339. xchr[@'41]:='!';
  340. xchr[@'42]:='"';
  341. xchr[@'43]:='#';
  342. xchr[@'44]:='$';
  343. xchr[@'45]:='%';
  344. xchr[@'46]:='&';
  345. xchr[@'47]:='''';@/
  346. xchr[@'50]:='(';
  347. xchr[@'51]:=')';
  348. xchr[@'52]:='*';
  349. xchr[@'53]:='+';
  350. xchr[@'54]:=',';
  351. xchr[@'55]:='-';
  352. xchr[@'56]:='.';
  353. xchr[@'57]:='/';@/
  354. xchr[@'60]:='0';
  355. xchr[@'61]:='1';
  356. xchr[@'62]:='2';
  357. xchr[@'63]:='3';
  358. xchr[@'64]:='4';
  359. xchr[@'65]:='5';
  360. xchr[@'66]:='6';
  361. xchr[@'67]:='7';@/
  362. xchr[@'70]:='8';
  363. xchr[@'71]:='9';
  364. xchr[@'72]:=':';
  365. xchr[@'73]:=';';
  366. xchr[@'74]:='<';
  367. xchr[@'75]:='=';
  368. xchr[@'76]:='>';
  369. xchr[@'77]:='?';@/
  370. xchr[@'100]:='@@';
  371. xchr[@'101]:='A';
  372. xchr[@'102]:='B';
  373. xchr[@'103]:='C';
  374. xchr[@'104]:='D';
  375. xchr[@'105]:='E';
  376. xchr[@'106]:='F';
  377. xchr[@'107]:='G';@/
  378. xchr[@'110]:='H';
  379. xchr[@'111]:='I';
  380. xchr[@'112]:='J';
  381. xchr[@'113]:='K';
  382. xchr[@'114]:='L';
  383. xchr[@'115]:='M';
  384. xchr[@'116]:='N';
  385. xchr[@'117]:='O';@/
  386. xchr[@'120]:='P';
  387. xchr[@'121]:='Q';
  388. xchr[@'122]:='R';
  389. xchr[@'123]:='S';
  390. xchr[@'124]:='T';
  391. xchr[@'125]:='U';
  392. xchr[@'126]:='V';
  393. xchr[@'127]:='W';@/
  394. xchr[@'130]:='X';
  395. xchr[@'131]:='Y';
  396. xchr[@'132]:='Z';
  397. xchr[@'133]:='[';
  398. xchr[@'134]:='\';
  399. xchr[@'135]:=']';
  400. xchr[@'136]:='^';
  401. xchr[@'137]:='_';@/
  402. xchr[@'140]:='`';
  403. xchr[@'141]:='a';
  404. xchr[@'142]:='b';
  405. xchr[@'143]:='c';
  406. xchr[@'144]:='d';
  407. xchr[@'145]:='e';
  408. xchr[@'146]:='f';
  409. xchr[@'147]:='g';@/
  410. xchr[@'150]:='h';
  411. xchr[@'151]:='i';
  412. xchr[@'152]:='j';
  413. xchr[@'153]:='k';
  414. xchr[@'154]:='l';
  415. xchr[@'155]:='m';
  416. xchr[@'156]:='n';
  417. xchr[@'157]:='o';@/
  418. xchr[@'160]:='p';
  419. xchr[@'161]:='q';
  420. xchr[@'162]:='r';
  421. xchr[@'163]:='s';
  422. xchr[@'164]:='t';
  423. xchr[@'165]:='u';
  424. xchr[@'166]:='v';
  425. xchr[@'167]:='w';@/
  426. xchr[@'170]:='x';
  427. xchr[@'171]:='y';
  428. xchr[@'172]:='z';
  429. xchr[@'173]:='{';
  430. xchr[@'174]:='|';
  431. xchr[@'175]:='}';
  432. xchr[@'176]:='~';
  433. for i:=@'177 to 255 do xchr[i]:='?';
  434. @ The following system-independent code makes the |xord| array contain a
  435. suitable inverse to the information in |xchr|.
  436. @<Set init...@>=
  437. for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
  438. for i:=" " to "~" do xord[xchr[i]]:=i;
  439. @* Reporting errors to the user.
  440. The \.{\title} processor does not verify that every single bit read from
  441. one of its binary input files is meaningful and consistent; there are
  442. other programs, e.g., \.{DVItype}, \.{TFtoPL}, and \.{VFtoPL}, specially
  443. designed for that purpose.
  444. On the other hand, \.{\title} is designed to avoid unpredictable results
  445. due to undetected arithmetic overflow, or due to violation of integer
  446. subranges or array bounds under {\it all\/} circumstances. Thus a fair
  447. amount of checking is done when reading and analyzing the input data,
  448. even in cases where such checking reduces the efficiency of the program
  449. to some extent.
  450. The error recovery capabilities of \.{\title} are, at least for the
  451. moment, extremely limited; everything worse than a warning message leads
  452. to the immediate termination of the program.
  453. @ If an input (\.{DVI}, \.{TFM}, \.{VF}, or other) file is badly malformed,
  454. the whole process must be aborted; \.{\title} will give up, after issuing
  455. an error message about what caused the error. These messages will, however,
  456. in most cases just indicate which input file caused the error. One of the
  457. programs \.{DVItype}, \.{TFtoPL} or \.{VFtoVP} should then be used to
  458. diagnose the error in full detail.
  459. Such errors might be discovered inside of subroutines inside of subroutines,
  460. so a procedure called |jump_out| has been introduced. This procedure, which
  461. transfers control to the label |final_end| at the end of the program,
  462. contains the only non-local |@!goto| statement in \.{DVIcopy}.
  463. @^system dependencies@>
  464. Some \PASCAL\ compilers do not implement non-local |goto| statements. In
  465. such cases the |goto final_end| in |jump_out| should simply be replaced
  466. by a call on some system procedure that quietly terminates the program.
  467. @^system dependencies@>
  468. @d abort(#)==begin print_ln(' ',#,'.'); jump_out;
  469.     end
  470. @<Error handling...@>=
  471. @<Basic printing procedures@>@;
  472. procedure close_files_and_terminate; forward;
  473. procedure jump_out;
  474. begin mark_fatal; close_files_and_terminate;
  475. goto final_end;
  476. @ Sometimes the program's behavior is far different from what it should
  477. be, and \.{\title} prints an error message that is really for the
  478. \.{\title} maintenance person, not the user. In such cases the program
  479. says |confusion(|indication of where we are|)|.
  480. @<Error handling...@>=
  481. procedure confusion(@!p:pckt_pointer);
  482. begin print(' !This can''t happen ('); print_packet(p); print_ln(').');
  483. @.This can't happen@>
  484. jump_out;
  485. @ An overflow stop occurs if \.{\title}'s tables aren't large enough.
  486. @<Error handling...@>=
  487. procedure overflow(@!p:pckt_pointer;@!n:int_16u);
  488. begin print(' !Sorry, DVIcopy capacity exceeded ['); print_packet(p);
  489. @.Sorry, DVIcopy capacity exceeded@>
  490. print_ln('=',n:1,'].');
  491. jump_out;
  492. @ If an attempt is made to store a second character packet for
  493. extension~|ext|, we give a warning message.
  494. @p procedure dup_warning(@!ext:int_24);
  495. begin if d_warn_count<10 then {stop telling after first 10 times}
  496.   begin print_ln('---duplicate character packet for extension ',ext:1);
  497. @.duplicate character packet@>
  498.   incr(d_warn_count); mark_harmless;
  499.   if d_warn_count=10 then print_ln('---further messages suppressed.');
  500.   end;
  501. @ If there are no character packets (with any extension) for character
  502. residue~|cur_res| and font~|cur_fnt|, we give a warning message.
  503. @p procedure pckt_warning;
  504. begin if p_warn_count<10 then {stop telling after first 10 times}
  505.   begin print_ln('---missing character packet character ',cur_res:1,
  506.     ' from font ',cur_fnt:1);
  507. @.missing character packet@>
  508.   incr(p_warn_count); mark_error;
  509.   if p_warn_count=10 then print_ln('---further messages suppressed.');
  510.   end;
  511. @ If a character packet for extension~|e| is used instead of one for
  512. extension~|ext| (which could not be found), we give a warning message.
  513. @p procedure subst_warning(@!e,@!ext:int_24);
  514. begin if s_warn_count<10 then {stop telling after first 10 times}
  515.   begin print_ln('---substituted character packet for extension ',
  516.     e:1,' instead of ',ext:1);
  517. @.substituted character packet@>
  518.   incr(s_warn_count); mark_error;
  519.   if s_warn_count=10 then print_ln('---further messages suppressed.');
  520.   end;
  521. @ @<Glob...@>=
  522. @!d_warn_count:int_7; {counts |dup_warning| messages}
  523. @!p_warn_count:int_7; {counts |pckt_warning| messages}
  524. @!s_warn_count:int_7; {counts |subst_warning| messages}
  525. @ @<Set init...@>=
  526. d_warn_count:=0; p_warn_count:=0; s_warn_count:=0;
  527. @* Device-independent file format.
  528. Before we get into the details of \.{\title}, we need to know exactly
  529. what \.{DVI} files are. The form of such files was designed by David R.
  530. @^Fuchs, David Raymond@>
  531. Fuchs in 1979. Almost any reasonable typesetting device can be driven by
  532. a program that takes \.{DVI} files as input, and dozens of such
  533. \.{DVI}-to-whatever programs have been written. Thus, it is possible to
  534. print the output of document compilers like \TeX\ on many different kinds
  535. of equipment.
  536. A \.{DVI} file is a stream of 8-bit bytes, which may be regarded as a
  537. series of commands in a machine-like language. The first byte of each command
  538. is the operation code, and this code is followed by zero or more bytes
  539. that provide parameters to the command. The parameters themselves may consist
  540. of several consecutive bytes; for example, the `|set_rule|' command has two
  541. parameters, each of which is four bytes long. Parameters are usually
  542. regarded as nonnegative integers; but four-byte-long parameters,
  543. and shorter parameters that denote distances, can be
  544. either positive or negative. Such parameters are given in two's complement
  545. notation. For example, a two-byte-long distance parameter has a value between
  546. $-2^{15}$ and $2^{15}-1$.
  547. @.DVI {\rm files}@>
  548. A \.{DVI} file consists of a ``preamble,'' followed by a sequence of one
  549. or more ``pages,'' followed by a ``postamble.'' The preamble is simply a
  550. |pre| command, with its parameters that define the dimensions used in the
  551. file; this must come first.  Each ``page'' consists of a |bop| command,
  552. followed by any number of other commands that tell where characters are to
  553. be placed on a physical page, followed by an |eop| command. The pages
  554. appear in the order that they were generated, not in any particular
  555. numerical order. If we ignore |nop| commands and \\{fnt\_def} commands
  556. (which are allowed between any two commands in the file), each |eop|
  557. command is immediately followed by a |bop| command, or by a |post|
  558. command; in the latter case, there are no more pages in the file, and the
  559. remaining bytes form the postamble.  Further details about the postamble
  560. will be explained later.
  561. Some parameters in \.{DVI} commands are ``pointers.'' These are four-byte
  562. quantities that give the location number of some other byte in the file;
  563. the first byte is number~0, then comes number~1, and so on. For example,
  564. one of the parameters of a |bop| command points to the previous |bop|;
  565. this makes it feasible to read the pages in backwards order, in case the
  566. results are being directed to a device that stacks its output face up.
  567. Suppose the preamble of a \.{DVI} file occupies bytes 0 to 99. Now if the
  568. first page occupies bytes 100 to 999, say, and if the second
  569. page occupies bytes 1000 to 1999, then the |bop| that starts in byte 1000
  570. points to 100 and the |bop| that starts in byte 2000 points to 1000. (The
  571. very first |bop|, i.e., the one that starts in byte 100, has a pointer of $-1$.)
  572. @ The \.{DVI} format is intended to be both compact and easily interpreted
  573. by a machine. Compactness is achieved by making most of the information
  574. implicit instead of explicit. When a \.{DVI}-reading program reads the
  575. commands for a page, it keeps track of several quantities: (a)~The current
  576. font |f| is an integer; this value is changed only
  577. by \\{fnt} and \\{fnt\_num} commands. (b)~The current position on the page
  578. is given by two numbers called the horizontal and vertical coordinates,
  579. |h| and |v|. Both coordinates are zero at the upper left corner of the page;
  580. moving to the right corresponds to increasing the horizontal coordinate, and
  581. moving down corresponds to increasing the vertical coordinate. Thus, the
  582. coordinates are essentially Cartesian, except that vertical directions are
  583. flipped; the Cartesian version of |(h,v)| would be |(h,-v)|.  (c)~The
  584. current spacing amounts are given by four numbers |w|, |x|, |y|, and |z|,
  585. where |w| and~|x| are used for horizontal spacing and where |y| and~|z|
  586. are used for vertical spacing. (d)~There is a stack containing
  587. |(h,v,w,x,y,z)| values; the \.{DVI} commands |push| and |pop| are used to
  588. change the current level of operation. Note that the current font~|f| is
  589. not pushed and popped; the stack contains only information about
  590. positioning.
  591. The values of |h|, |v|, |w|, |x|, |y|, and |z| are signed integers having up
  592. to 32 bits, including the sign. Since they represent physical distances,
  593. there is a small unit of measurement such that increasing |h| by~1 means
  594. moving a certain tiny distance to the right. The actual unit of
  595. measurement is variable, as explained below.
  596. @ Here is a list of all the commands that may appear in a \.{DVI} file. Each
  597. command is specified by its symbolic name (e.g., |bop|), its opcode byte
  598. (e.g., 139), and its parameters (if any). The parameters are followed
  599. by a bracketed number telling how many bytes they occupy; for example,
  600. `|p[4]|' means that parameter |p| is four bytes long.
  601. \yskip\hang|set_char_0| 0. Typeset character number~0 from font~|f|
  602. such that the reference point of the character is at |(h,v)|. Then
  603. increase |h| by the width of that character. Note that a character may
  604. have zero or negative width, so one cannot be sure that |h| will advance
  605. after this command; but |h| usually does increase.
  606. \yskip\hang|set_char_1| through |set_char_127| (opcodes 1 to 127).
  607. Do the operations of |set_char_0|; but use the character whose number
  608. matches the opcode, instead of character~0.
  609. \yskip\hang|set1| 128 |c[1]|. Same as |set_char_0|, except that character
  610. number~|c| is typeset. \TeX82 uses this command for characters in the
  611. range |128<=c<256|.
  612. \yskip\hang|set2| 129 |c[2]|. Same as |set1|, except that |c|~is two
  613. bytes long, so it is in the range |0<=c<65536|. \TeX82 never uses this
  614. command, which is intended for processors that deal with oriental languages;
  615. but \.{\title} will allow character codes greater than 255, assuming that
  616. they all have the same width as the character whose code is $c \bmod 256$.
  617. @^oriental characters@>@^Chinese characters@>@^Japanese characters@>
  618. \yskip\hang|set3| 130 |c[3]|. Same as |set1|, except that |c|~is three
  619. bytes long, so it can be as large as $2^{24}-1$.
  620. \yskip\hang|set4| 131 |c[4]|. Same as |set1|, except that |c|~is four
  621. bytes long, possibly even negative. Imagine that.
  622. \yskip\hang|set_rule| 132 |a[4]| |b[4]|. Typeset a solid black rectangle
  623. of height |a| and width |b|, with its bottom left corner at |(h,v)|. Then
  624. set |h:=h+b|. If either |a<=0| or |b<=0|, nothing should be typeset. Note
  625. that if |b<0|, the value of |h| will decrease even though nothing else happens.
  626. Programs that typeset from \.{DVI} files should be careful to make the rules
  627. line up carefully with digitized characters, as explained in connection with
  628. the |rule_pixels| subroutine below.
  629. \yskip\hang|put1| 133 |c[1]|. Typeset character number~|c| from font~|f|
  630. such that the reference point of the character is at |(h,v)|. (The `put'
  631. commands are exactly like the `set' commands, except that they simply put out a
  632. character or a rule without moving the reference point afterwards.)
  633. \yskip\hang|put2| 134 |c[2]|. Same as |set2|, except that |h| is not changed.
  634. \yskip\hang|put3| 135 |c[3]|. Same as |set3|, except that |h| is not changed.
  635. \yskip\hang|put4| 136 |c[4]|. Same as |set4|, except that |h| is not changed.
  636. \yskip\hang|put_rule| 137 |a[4]| |b[4]|. Same as |set_rule|, except that
  637. |h| is not changed.
  638. \yskip\hang|nop| 138. No operation, do nothing. Any number of |nop|'s
  639. may occur between \.{DVI} commands, but a |nop| cannot be inserted between
  640. a command and its parameters or between two parameters.
  641. \yskip\hang|bop| 139 $c_0[4]$ $c_1[4]$ $\ldots$ $c_9[4]$ $p[4]$. Beginning
  642. of a page: Set |(h,v,w,x,y,z):=(0,0,0,0,0,0)| and set the stack empty. Set
  643. the current font |f| to an undefined value.  The ten $c_i$ parameters can
  644. be used to identify pages, if a user wants to print only part of a \.{DVI}
  645. file; \TeX82 gives them the values of \.{\\count0} $\ldots$ \.{\\count9}
  646. at the time \.{\\shipout} was invoked for this page.  The parameter |p|
  647. points to the previous |bop| command in the file, where the first |bop|
  648. has $p=-1$.
  649. \yskip\hang|eop| 140.  End of page: Print what you have read since the
  650. previous |bop|. At this point the stack should be empty. (The \.{DVI}-reading
  651. programs that drive most output devices will have kept a buffer of the
  652. material that appears on the page that has just ended. This material is
  653. largely, but not entirely, in order by |v| coordinate and (for fixed |v|) by
  654. |h|~coordinate; so it usually needs to be sorted into some order that is
  655. appropriate for the device in question. \.{\title} does not do such sorting.)
  656. \yskip\hang|push| 141. Push the current values of |(h,v,w,x,y,z)| onto the
  657. top of the stack; do not change any of these values. Note that |f| is
  658. not pushed.
  659. \yskip\hang|pop| 142. Pop the top six values off of the stack and assign
  660. them to |(h,v,w,x,y,z)|. The number of pops should never exceed the number
  661. of pushes, since it would be highly embarrassing if the stack were empty
  662. at the time of a |pop| command.
  663. \yskip\hang|right1| 143 |b[1]|. Set |h:=h+b|, i.e., move right |b| units.
  664. The parameter is a signed number in two's complement notation, |-128<=b<128|;
  665. if |b<0|, the reference point actually moves left.
  666. \yskip\hang|right2| 144 |b[2]|. Same as |right1|, except that |b| is a
  667. two-byte quantity in the range |-32768<=b<32768|.
  668. \yskip\hang|right3| 145 |b[3]|. Same as |right1|, except that |b| is a
  669. three-byte quantity in the range |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  670. \yskip\hang|right4| 146 |b[4]|. Same as |right1|, except that |b| is a
  671. four-byte quantity in the range |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  672. \yskip\hang|w0| 147. Set |h:=h+w|; i.e., move right |w| units. With luck,
  673. this parameterless command will usually suffice, because the same kind of motion
  674. will occur several times in succession; the following commands explain how
  675. |w| gets particular values.
  676. \yskip\hang|w1| 148 |b[1]|. Set |w:=b| and |h:=h+b|. The value of |b| is a
  677. signed quantity in two's complement notation, |-128<=b<128|. This command
  678. changes the current |w|~spacing and moves right by |b|.
  679. \yskip\hang|w2| 149 |b[2]|. Same as |w1|, but |b| is a two-byte-long
  680. parameter, |-32768<=b<32768|.
  681. \yskip\hang|w3| 150 |b[3]|. Same as |w1|, but |b| is a three-byte-long
  682. parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  683. \yskip\hang|w4| 151 |b[4]|. Same as |w1|, but |b| is a four-byte-long
  684. parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  685. \yskip\hang|x0| 152. Set |h:=h+x|; i.e., move right |x| units. The `|x|'
  686. commands are like the `|w|' commands except that they involve |x| instead
  687. of |w|.
  688. \yskip\hang|x1| 153 |b[1]|. Set |x:=b| and |h:=h+b|. The value of |b| is a
  689. signed quantity in two's complement notation, |-128<=b<128|. This command
  690. changes the current |x|~spacing and moves right by |b|.
  691. \yskip\hang|x2| 154 |b[2]|. Same as |x1|, but |b| is a two-byte-long
  692. parameter, |-32768<=b<32768|.
  693. \yskip\hang|x3| 155 |b[3]|. Same as |x1|, but |b| is a three-byte-long
  694. parameter, |@t$-2^{23}$@><=b<@t$2^{23}$@>|.
  695. \yskip\hang|x4| 156 |b[4]|. Same as |x1|, but |b| is a four-byte-long
  696. parameter, |@t$-2^{31}$@><=b<@t$2^{31}$@>|.
  697. \yskip\hang|down1| 157 |a[1]|. Set |v:=v+a|, i.e., move down |a| units.
  698. The parameter is a signed number in two's complement notation, |-128<=a<128|;
  699. if |a<0|, the reference point actually moves up.
  700. \yskip\hang|down2| 158 |a[2]|. Same as |down1|, except that |a| is a
  701. two-byte quantity in the range |-32768<=a<32768|.
  702. \yskip\hang|down3| 159 |a[3]|. Same as |down1|, except that |a| is a
  703. three-byte quantity in the range |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  704. \yskip\hang|down4| 160 |a[4]|. Same as |down1|, except that |a| is a
  705. four-byte quantity in the range |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  706. \yskip\hang|y0| 161. Set |v:=v+y|; i.e., move down |y| units. With luck,
  707. this parameterless command will usually suffice, because the same kind of motion
  708. will occur several times in succession; the following commands explain how
  709. |y| gets particular values.
  710. \yskip\hang|y1| 162 |a[1]|. Set |y:=a| and |v:=v+a|. The value of |a| is a
  711. signed quantity in two's complement notation, |-128<=a<128|. This command
  712. changes the current |y|~spacing and moves down by |a|.
  713. \yskip\hang|y2| 163 |a[2]|. Same as |y1|, but |a| is a two-byte-long
  714. parameter, |-32768<=a<32768|.
  715. \yskip\hang|y3| 164 |a[3]|. Same as |y1|, but |a| is a three-byte-long
  716. parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  717. \yskip\hang|y4| 165 |a[4]|. Same as |y1|, but |a| is a four-byte-long
  718. parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  719. \yskip\hang|z0| 166. Set |v:=v+z|; i.e., move down |z| units. The `|z|' commands
  720. are like the `|y|' commands except that they involve |z| instead of |y|.
  721. \yskip\hang|z1| 167 |a[1]|. Set |z:=a| and |v:=v+a|. The value of |a| is a
  722. signed quantity in two's complement notation, |-128<=a<128|. This command
  723. changes the current |z|~spacing and moves down by |a|.
  724. \yskip\hang|z2| 168 |a[2]|. Same as |z1|, but |a| is a two-byte-long
  725. parameter, |-32768<=a<32768|.
  726. \yskip\hang|z3| 169 |a[3]|. Same as |z1|, but |a| is a three-byte-long
  727. parameter, |@t$-2^{23}$@><=a<@t$2^{23}$@>|.
  728. \yskip\hang|z4| 170 |a[4]|. Same as |z1|, but |a| is a four-byte-long
  729. parameter, |@t$-2^{31}$@><=a<@t$2^{31}$@>|.
  730. \yskip\hang|fnt_num_0| 171. Set |f:=0|. Font 0 must previously have been
  731. defined by a \\{fnt\_def} instruction, as explained below.
  732. \yskip\hang|fnt_num_1| through |fnt_num_63| (opcodes 172 to 234). Set
  733. |f:=1|, \dots, |f:=63|, respectively.
  734. \yskip\hang|fnt1| 235 |k[1]|. Set |f:=k|. \TeX82 uses this command for font
  735. numbers in the range |64<=k<256|.
  736. \yskip\hang|fnt2| 236 |k[2]|. Same as |fnt1|, except that |k|~is two
  737. bytes long, so it is in the range |0<=k<65536|. \TeX82 never generates this
  738. command, but large font numbers may prove useful for specifications of
  739. color or texture, or they may be used for special fonts that have fixed
  740. numbers in some external coding scheme.
  741. \yskip\hang|fnt3| 237 |k[3]|. Same as |fnt1|, except that |k|~is three
  742. bytes long, so it can be as large as $2^{24}-1$.
  743. \yskip\hang|fnt4| 238 |k[4]|. Same as |fnt1|, except that |k|~is four
  744. bytes long; this is for the really big font numbers (and for the negative ones).
  745. \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in
  746. general; it functions as a $(k+2)$-byte |nop| unless special \.{DVI}-reading
  747. programs are being used. \TeX82 generates |xxx1| when a short enough
  748. \.{\\special} appears, setting |k| to the number of bytes being sent. It
  749. is recommended that |x| be a string having the form of a keyword followed
  750. by possible parameters relevant to that keyword.
  751. \yskip\hang|xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|.
  752. \yskip\hang|xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2^{24}$@>|.
  753. \yskip\hang|xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously
  754. large. \TeX82 uses |xxx4| when |xxx1| would be incorrect.
  755. \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  756. Define font |k|, where |0<=k<256|; font definitions will be explained shortly.
  757. \yskip\hang|fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  758. Define font |k|, where |0<=k<65536|.
  759. \yskip\hang|fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  760. Define font |k|, where |0<=k<@t$2^{24}$@>|.
  761. \yskip\hang|fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  762. Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
  763. \yskip\hang|pre| 247 |i[1]| |num[4]| |den[4]| |mag[4]| |k[1]| |x[k]|.
  764. Beginning of the preamble; this must come at the very beginning of the
  765. file. Parameters |i|, |num|, |den|, |mag|, |k|, and |x| are explained below.
  766. \yskip\hang|post| 248. Beginning of the postamble, see below.
  767. \yskip\hang|post_post| 249. Ending of the postamble, see below.
  768. \yskip\noindent Commands 250--255 are undefined at the present time.
  769. @ @d set_char_0=0 {typeset character 0 and move right}
  770. @d set1=128 {typeset a character and move right}
  771. @d set_rule=132 {typeset a rule and move right}
  772. @d put1=133 {typeset a character}
  773. @d put_rule=137 {typeset a rule}
  774. @d nop=138 {no operation}
  775. @d bop=139 {beginning of page}
  776. @d eop=140 {ending of page}
  777. @d push=141 {save the current positions}
  778. @d pop=142 {restore previous positions}
  779. @d right1=143 {move right}
  780. @d w0=147 {move right by |w|}
  781. @d w1=148 {move right and set |w|}
  782. @d x0=152 {move right by |x|}
  783. @d x1=153 {move right and set |x|}
  784. @d down1=157 {move down}
  785. @d y0=161 {move down by |y|}
  786. @d y1=162 {move down and set |y|}
  787. @d z0=166 {move down by |z|}
  788. @d z1=167 {move down and set |z|}
  789. @d fnt_num_0=171 {set current font to 0}
  790. @d fnt1=235 {set current font}
  791. @d xxx1=239 {extension to \.{DVI} primitives}
  792. @d xxx4=242 {potentially long extension to \.{DVI} primitives}
  793. @d fnt_def1=243 {define the meaning of a font number}
  794. @d pre=247 {preamble}
  795. @d post=248 {postamble beginning}
  796. @d post_post=249 {postamble ending}
  797. @d undefined_commands==250,251,252,253,254,255
  798. @ The preamble contains basic information about the file as a whole. As
  799. stated above, there are six parameters:
  800. $$\hbox{|@!i[1]| |@!num[4]| |@!den[4]| |@!mag[4]| |@!k[1]| |@!x[k]|.}$$
  801. The |i| byte identifies \.{DVI} format; currently this byte is always set
  802. to~2. (The value |i=3| is currently used for an extended format that
  803. allows a mixture of right-to-left and left-to-right typesetting.
  804. Some day we will set |i=4|, when \.{DVI} format makes another
  805. incompatible change---perhaps in the year 2048.)
  806. The next two parameters, |num| and |den|, are positive integers that define
  807. the units of measurement; they are the numerator and denominator of a
  808. fraction by which all dimensions in the \.{DVI} file could be multiplied
  809. in order to get lengths in units of $10^{-7}$ meters. (For example, there are
  810. exactly 7227 \TeX\ points in 254 centimeters, and \TeX82 works with scaled
  811. points where there are $2^{16}$ sp in a point, so \TeX82 sets |num=25400000|
  812. and $|den|=7227\cdot2^{16}=473628672$.)
  813. @^sp@>
  814. The |mag| parameter is what \TeX82 calls \.{\\mag}, i.e., 1000 times the
  815. desired magnification. The actual fraction by which dimensions are
  816. multiplied is therefore $mn/1000d$. Note that if a \TeX\ source document
  817. does not call for any `\.{true}' dimensions, and if you change it only by
  818. specifying a different \.{\\mag} setting, the \.{DVI} file that \TeX\
  819. creates will be completely unchanged except for the value of |mag| in the
  820. preamble and postamble. (Fancy \.{DVI}-reading programs allow users to
  821. override the |mag|~setting when a \.{DVI} file is being printed.)
  822. Finally, |k| and |x| allow the \.{DVI} writer to include a comment, which is not
  823. interpreted further. The length of comment |x| is |k|, where |0<=k<256|.
  824. @d dvi_id=2 {identifies the kind of \.{DVI} files described here}
  825. @ Font definitions for a given font number |k| contain further parameters
  826. $$\hbox{|c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.}$$
  827. The four-byte value |c| is the check sum that \TeX\ (or whatever program
  828. generated the \.{DVI} file) found in the \.{TFM} file for this font;
  829. |c| should match the check sum of the font found by programs that read
  830. this \.{DVI} file.
  831. @^check sum@>
  832. Parameter |s| contains a fixed-point scale factor that is applied to the
  833. character widths in font |k|; font dimensions in \.{TFM} files and other
  834. font files are relative to this quantity, which is always positive and
  835. less than $2^{27}$. It is given in the same units as the other dimensions
  836. of the \.{DVI} file.  Parameter |d| is similar to |s|; it is the ``design
  837. size,'' and (like~|s|) it is given in \.{DVI} units. Thus, font |k| is to be
  838. used at $|mag|\cdot s/1000d$ times its normal size.
  839. The remaining part of a font definition gives the external name of the font,
  840. which is an ASCII string of length |a+l|. The number |a| is the length
  841. of the ``area'' or directory, and |l| is the length of the font name itself;
  842. the standard local system font area is supposed to be used when |a=0|.
  843. The |n| field contains the area in its first |a| bytes.
  844. Font definitions must appear before the first use of a particular font number.
  845. Once font |k| is defined, it must not be defined again; however, we
  846. shall see below that font definitions appear in the postamble as well as
  847. in the pages, so in this sense each font number is defined exactly twice,
  848. if at all. Like |nop| commands and \\{xxx} commands, font definitions can
  849. appear before the first |bop|, or between an |eop| and a |bop|.
  850. @ The last page in a \.{DVI} file is followed by `|post|'; this command
  851. introduces the postamble, which summarizes important facts that \TeX\ has
  852. accumulated about the file, making it possible to print subsets of the data
  853. with reasonable efficiency. The postamble has the form
  854. $$\vbox{\halign{\hbox{#\hfil}\cr
  855.   |post| |p[4]| |num[4]| |den[4]| |mag[4]| |l[4]| |u[4]| |s[2]| |t[2]|\cr
  856.   $\langle\,$font definitions$\,\rangle$\cr
  857.   |post_post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$
  858. Here |p| is a pointer to the final |bop| in the file. The next three
  859. parameters, |num|, |den|, and |mag|, are duplicates of the quantities that
  860. appeared in the preamble.
  861. Parameters |l| and |u| give respectively the height-plus-depth of the tallest
  862. page and the width of the widest page, in the same units as other dimensions
  863. of the file. These numbers might be used by a \.{DVI}-reading program to
  864. position individual ``pages'' on large sheets of film or paper; however,
  865. the standard convention for output on normal size paper is to position each
  866. page so that the upper left-hand corner is exactly one inch from the left
  867. and the top. Experience has shown that it is unwise to design \.{DVI}-to-printer
  868. software that attempts cleverly to center the output; a fixed position of
  869. the upper left corner is easiest for users to understand and to work with.
  870. Therefore |l| and~|u| are often ignored.
  871. Parameter |s| is the maximum stack depth (i.e., the largest excess of
  872. |push| commands over |pop| commands) needed to process this file. Then
  873. comes |t|, the total number of pages (|bop| commands) present.
  874. The postamble continues with font definitions, which are any number of
  875. \\{fnt\_def} commands as described above, possibly interspersed with |nop|
  876. commands. Each font number that is used in the \.{DVI} file must be defined
  877. exactly twice: Once before it is first selected by a \\{fnt} command, and once
  878. in the postamble.
  879. @ The last part of the postamble, following the |post_post| byte that
  880. signifies the end of the font definitions, contains |q|, a pointer to the
  881. |post| command that started the postamble.  An identification byte, |i|,
  882. comes next; this currently equals~2, as in the preamble.
  883. The |i| byte is followed by four or more bytes that are all equal to
  884. the decimal number 223 (i.e., @'337 in octal). \TeX\ puts out four to seven of
  885. these trailing bytes, until the total length of the file is a multiple of
  886. four bytes, since this works out best on machines that pack four bytes per
  887. word; but any number of 223's is allowed, as long as there are at least four
  888. of them. In effect, 223 is a sort of signature that is added at the very end.
  889. @^Fuchs, David Raymond@>
  890. This curious way to finish off a \.{DVI} file makes it feasible for
  891. \.{DVI}-reading programs to find the postamble first, on most computers,
  892. even though \TeX\ wants to write the postamble last. Most operating
  893. systems permit random access to individual words or bytes of a file, so
  894. the \.{DVI} reader can start at the end and skip backwards over the 223's
  895. until finding the identification byte. Then it can back up four bytes, read
  896. |q|, and move to byte |q| of the file. This byte should, of course,
  897. contain the value 248 (|post|); now the postamble can be read, so the
  898. \.{DVI} reader discovers all the information needed for typesetting the
  899. pages. Note that it is also possible to skip through the \.{DVI} file at
  900. reasonably high speed to locate a particular page, if that proves
  901. desirable. This saves a lot of time, since \.{DVI} files used in production
  902. jobs tend to be large.
  903. Unfortunately, however, standard \PASCAL\ does not include the ability to
  904. @^system dependencies@>
  905. access a random position in a file, or even to determine the length of a file.
  906. Almost all systems nowadays provide the necessary capabilities, so \.{DVI}
  907. format has been designed to work most efficiently with modern operating systems.
  908. As noted above, \.{\title} will limit itself to the restrictions of standard
  909. \PASCAL\ if |random_reading| is defined to be |false|.
  910. @* Virtual fonts.
  911. Before we get into the details of \.{\title}, we need to know exactly
  912. what \.{VF} files are. Inspired by earlier work of David~R. Fuchs back
  913. @^Fuchs, David Raymond@>
  914. in 1984, the form of such files was designed by Donald~E. Knuth in 1989.
  915. @^Knuth, Donald Ervin@>
  916. The idea behind \.{VF} files is that a general
  917. interface mechanism is needed to switch between the myriad font
  918. layouts provided by different suppliers of typesetting equipment.
  919. Without such a mechanism, people must go to great lengths writing
  920. inscrutable macros whenever they want to use typesetting conventions
  921. based on one font layout in connection with actual fonts that have
  922. another layout. This puts an extra burden on the typesetting system,
  923. interfering with the other things it needs to do (like kerning,
  924. hyphenation, and ligature formation).
  925. These difficulties go away when we have a ``virtual font,''
  926. i.e., a font that exists in a logical sense but not a physical sense.
  927. A typesetting system like \TeX\ can do its job without knowing where the
  928. actual characters come from; a device driver can then do its job by
  929. letting a \.{VF} file tell what actual characters correspond to the
  930. characters \TeX\ imagined were present. The actual characters
  931. can be shifted and/or magnified and/or combined with other characters
  932. from many different fonts. A virtual font can even make use of characters
  933. from virtual fonts, including itself.
  934. Virtual fonts also allow convenient character substitutions for proofreading
  935. purposes, when fonts designed for one output device are unavailable on another.
  936. @ A \.{VF} file is organized as a stream of 8-bit bytes, using conventions
  937. borrowed from \.{DVI} and \.{PK} files. Thus, a device driver that knows
  938. about \.{DVI} and \.{PK} format will already
  939. contain most of the mechanisms necessary to process \.{VF} files.
  940. A preamble
  941. appears at the beginning, followed by a sequence of character definitions,
  942. followed by a postamble. More precisely, the first byte of every \.{VF} file
  943. must be the first byte of the following ``preamble command'':
  944. \yskip\hang|pre| 247 |i[1]| |k[1]| |x[k]| |cs[4]| |ds[4]|.
  945. Here |i| is the identification byte of \.{VF}, currently 202. The string
  946. |x| is merely a comment, usually indicating the source of the \.{VF} file.
  947. Parameters |cs| and |ds| are respectively the check sum and the design size
  948. of the virtual font; they should match the first two words in the header of
  949. the \.{TFM} file, as described above.
  950. \yskip
  951. After the |pre| command, the preamble continues with font definitions;
  952. every font needed to specify ``actual'' characters in later
  953. \\{set\_char} commands is defined here. The font definitions are
  954. exactly the same in \.{VF} files as they are in \.{DVI} files, except
  955. that the scaled size |s| is relative and the design size |d| is absolute:
  956. \yskip\hang|fnt_def1| 243 |k[1]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  957. Define font |k|, where |0<=k<256|.
  958. \yskip\hang|@!fnt_def2| 244 |k[2]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  959. Define font |k|, where |0<=k<65536|.
  960. \yskip\hang|@!fnt_def3| 245 |k[3]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  961. Define font |k|, where |0<=k<@t$2^{24}$@>|.
  962. \yskip\hang|@!fnt_def4| 246 |k[4]| |c[4]| |s[4]| |d[4]| |a[1]| |l[1]| |n[a+l]|.
  963. Define font |k|, where |@t$-2^{31}$@><=k<@t$2^{31}$@>|.
  964. \yskip\noindent
  965. These font numbers |k| are ``local''; they have no relation to font numbers
  966. defined in the \.{DVI} file that uses this virtual font. The dimension~|s|,
  967. which represents the scaled size of the local font being defined,
  968. is a |fix_word| relative to the design size of the virtual font.
  969. Thus if the local font is to be used at the same size
  970. as the design size of the virtual font itself, |s| will be the
  971. integer value $2^{20}$. The value of |s| must be positive and less than
  972. $2^{24}$ (thus less than 16 when considered as a |fix_word|).
  973. The dimension~|d| is a |fix_word| in units of printer's points; hence it
  974. is identical to the design size found in the corresponding \.{TFM} file.
  975. @d vf_id=202
  976. @ The preamble is followed by zero or more character packets, where each
  977. character packet begins with a byte that is $<243$. Character packets have
  978. two formats, one long and one short:
  979. \yskip\hang|long_char| 242 |pl[4]| |cc[4]| |tfm[4]| |dvi[pl]|. This long form
  980. specifies a virtual character in the general case.
  981. \yskip\hang|short_char0..short_char241|
  982. |pl[1]| |cc[1]| |tfm[3]| |dvi[pl]|. This short form specifies a
  983. virtual character in the common case
  984. when |0<=pl<242| and |0<=cc<256| and $0\le|tfm|<2^{24}$.
  985. \yskip\noindent
  986. Here |pl| denotes the packet length following the |tfm| value; |cc| is
  987. the character code; and |tfm| is the character width copied from the
  988. \.{TFM} file for this virtual font. There should be at most one character
  989. packet having any given |cc| code.
  990. The |dvi| bytes are a sequence of complete \.{DVI} commands, properly
  991. nested with respect to |push| and |pop|. All \.{DVI} operations are
  992. permitted except |bop|, |eop|, and commands with opcodes |>=243|.
  993. Font selection commands (|fnt_num0| through |fnt4|) must refer to fonts
  994. defined in the preamble.
  995. Dimensions that appear in the \.{DVI} instructions are analogous to
  996. |fix_word| quantities; i.e., they are integer multiples of $2^{-20}$ times
  997. the design size of the virtual font. For example, if the virtual font
  998. has design size $10\,$pt, the \.{DVI} command to move down $5\,$pt
  999. would be a \\{down} instruction with parameter $2^{19}$. The virtual font
  1000. itself might be used at a different size, say $12\,$pt; then that
  1001. \\{down} instruction would move down $6\,$pt instead. Each dimension
  1002. must be less than $2^{24}$ in absolute value.
  1003. Device drivers processing \.{VF} files treat the sequences of |dvi| bytes
  1004. as subroutines or macros, implicitly enclosing them with |push| and |pop|.
  1005. Each subroutine begins with |w=x=y=z=0|, and with current font~|f| the
  1006. number of the first-defined in the preamble (undefined if there's no
  1007. such font). After the |dvi| commands have been
  1008. performed, the |h| and~|v| position registers of \.{DVI} format and the
  1009. current font~|f| are restored to their former values;
  1010. then, if the subroutine has been invoked by a \\{set\_char} or \\{set}
  1011. command, |h|~is increased by the \.{TFM} width
  1012. (properly scaled)---just as if a simple character had been typeset.
  1013. @d long_char=242 {\.{VF} command for general character packet}
  1014. @d improper_DVI_for_VF==139,140,243,244,245,246,247,248,249,250,251,252,
  1015.     253,254,255
  1016. @ The character packets are followed by a trivial postamble, consisting of
  1017. one or more bytes all equal to |post| (248). The total number of bytes
  1018. in the file should be a multiple of~4.
  1019. @* Font metric data.
  1020. The idea behind \.{TFM} files is that typesetting routines like \TeX\
  1021. need a compact way to store the relevant information about several
  1022. dozen fonts, and computer centers need a compact way to store the
  1023. relevant information about several hundred fonts. \.{TFM} files are
  1024. compact, and most of the information they contain is highly relevant,
  1025. so they provide a solution to the problem.
  1026. The information in a \.{TFM} file appears in a sequence of 8-bit bytes.
  1027. Since the number of bytes is always a multiple of 4, we could
  1028. also regard the file as a sequence of 32-bit words; but \TeX\ uses the
  1029. byte interpretation, and so does \.{\title}. Note that the bytes
  1030. are considered to be unsigned numbers.
  1031. @ The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit
  1032. integers that give the lengths of the various subsequent portions
  1033. of the file. These twelve integers are, in order:
  1034. $$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr
  1035. |@!lf|&length of the entire file, in words;\cr
  1036. |@!lh|&length of the header data, in words;\cr
  1037. |@!bc|&smallest character code in the font;\cr
  1038. |@!ec|&largest character code in the font;\cr
  1039. |@!nw|&number of words in the width table;\cr
  1040. |@!nh|&number of words in the height table;\cr
  1041. |@!nd|&number of words in the depth table;\cr
  1042. |@!ni|&number of words in the italic correction table;\cr
  1043. |@!nl|&number of words in the lig/kern table;\cr
  1044. |@!nk|&number of words in the kern table;\cr
  1045. |@!ne|&number of words in the extensible character table;\cr
  1046. |@!np|&number of font parameter words.\cr}}$$
  1047. They are all nonnegative and less than $2^{15}$. We must have |bc-1<=ec<=255|,
  1048. |ne<=256|, and
  1049. $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$
  1050. Note that a font may contain as many as 256 characters (if |bc=0| and |ec=255|),
  1051. and as few as 0 characters (if |bc=ec+1|).
  1052. Incidentally, when two or more 8-bit bytes are combined to form an integer of
  1053. 16 or more bits, the most significant bytes appear first in the file.
  1054. This is called BigEndian order.
  1055. @ The rest of the \.{TFM} file may be regarded as a sequence of ten data
  1056. arrays having the informal specification
  1057. $$\def\arr$[#1]#2${\&{array} $[#1]$ \&{of} #2}
  1058. \vbox{\halign{\hfil\\{#}&$\,:\,$\arr#\hfil\cr
  1059. header&|[0..lh-1]stuff|\cr
  1060. char\_info&|[bc..ec]char_info_word|\cr
  1061. width&|[0..nw-1]fix_word|\cr
  1062. height&|[0..nh-1]fix_word|\cr
  1063. depth&|[0..nd-1]fix_word|\cr
  1064. italic&|[0..ni-1]fix_word|\cr
  1065. lig\_kern&|[0..nl-1]lig_kern_command|\cr
  1066. kern&|[0..nk-1]fix_word|\cr
  1067. exten&|[0..ne-1]extensible_recipe|\cr
  1068. param&|[1..np]fix_word|\cr}}$$
  1069. The most important data type used here is a |@!fix_word|, which is
  1070. a 32-bit representation of a binary fraction. A |fix_word| is a signed
  1071. quantity, with the two's complement of the entire word used to represent
  1072. negation. Of the 32 bits in a |fix_word|, exactly 12 are to the left of the
  1073. binary point; thus, the largest |fix_word| value is $2048-2^{-20}$, and
  1074. the smallest is $-2048$. We will see below, however, that all but one of
  1075. the |fix_word| values will lie between $-16$ and $+16$.
  1076. @ The first data array is a block of header information, which contains
  1077. general facts about the font. The header must contain at least two words,
  1078. and for \.{TFM} files to be used with Xerox printing software it must
  1079. contain at least 18 words, allocated as described below. When different
  1080. kinds of devices need to be interfaced, it may be necessary to add further
  1081. words to the header block.
  1082. \yskip\hang|header[0]| is a 32-bit check sum that \TeX\ will copy into the
  1083. \.{DVI} output file whenever it uses the font.  Later on when the \.{DVI}
  1084. file is printed, possibly on another computer, the actual font that gets
  1085. used is supposed to have a check sum that agrees with the one in the
  1086. \.{TFM} file used by \TeX. In this way, users will be warned about
  1087. potential incompatibilities. (However, if the check sum is zero in either
  1088. the font file or the \.{TFM} file, no check is made.)  The actual relation
  1089. between this check sum and the rest of the \.{TFM} file is not important;
  1090. the check sum is simply an identification number with the property that
  1091. incompatible fonts almost always have distinct check sums.
  1092. @^check sum@>
  1093. \yskip\hang|header[1]| is a |fix_word| containing the design size of the
  1094. font, in units of \TeX\ points (7227 \TeX\ points = 254 cm).  This number
  1095. must be at least 1.0; it is fairly arbitrary, but usually the design size
  1096. is 10.0 for a ``10 point'' font, i.e., a font that was designed to look
  1097. best at a 10-point size, whatever that really means. When a \TeX\ user
  1098. asks for a font `\.{at} $\delta$ \.{pt}', the effect is to override the
  1099. design size and replace it by $\delta$, and to multiply the $x$ and~$y$
  1100. coordinates of the points in the font image by a factor of $\delta$
  1101. divided by the design size.  {\sl All other dimensions in the\/\ \.{TFM}
  1102. file are |fix_word|\kern-1pt\ numbers in design-size units.} Thus, for example,
  1103. the value of |param[6]|, one \.{em} or \.{\\quad}, is often the |fix_word|
  1104. value $2^{20}=1.0$, since many fonts have a design size equal to one em.
  1105. The other dimensions must be less than 16 design-size units in absolute
  1106. value; thus, |header[1]| and |param[1]| are the only |fix_word| entries in
  1107. the whole \.{TFM} file whose first byte might be something besides 0 or
  1108. 255.  @^design size@>
  1109. \yskip\hang|header[2..11]|, if present, contains 40 bytes that identify
  1110. the character coding scheme. The first byte, which must be between 0 and
  1111. 39, is the number of subsequent ASCII bytes actually relevant in this
  1112. string, which is intended to specify what character-code-to-symbol
  1113. convention is present in the font.  Examples are \.{ASCII} for standard
  1114. ASCII, \.{TeX text} for fonts like \.{cmr10} and \.{cmti9}, \.{TeX math
  1115. extension} for \.{cmex10}, \.{XEROX text} for Xerox fonts, \.{GRAPHIC} for
  1116. special-purpose non-alphabetic fonts, \.{UNSPECIFIED} for the default case
  1117. when there is no information.  Parentheses should not appear in this name.
  1118. (Such a string is said to be in {\mc BCPL} format.)
  1119. @^coding scheme@>
  1120. \yskip\hang|header[12..16]|, if present, contains 20 bytes that name the
  1121. font family (e.g., \.{CMR} or \.{HELVETICA}), in {\mc BCPL} format.
  1122. This field is also known as the ``font identifier.''
  1123. @^family name@>
  1124. @^font identifier@>
  1125. \yskip\hang|header[17]|, if present, contains a first byte called the
  1126. |seven_bit_safe_flag|, then two bytes that are ignored, and a fourth byte
  1127. called the |face|. If the value of the fourth byte is less than 18, it has
  1128. the following interpretation as a ``weight, slope, and expansion'':  Add 0
  1129. or 2 or 4 (for medium or bold or light) to 0 or 1 (for roman or italic) to
  1130. 0 or 6 or 12 (for regular or condensed or extended).  For example, 13 is
  1131. 0+1+12, so it represents medium italic extended.  A three-letter code
  1132. (e.g., \.{MIE}) can be used for such |face| data.
  1133. \yskip\hang|header[18..@twhatever@>]| might also be present; the individual
  1134. words are simply called |header[18]|, |header[19]|, etc., at the moment.
  1135. @ Next comes the |char_info| array, which contains one |char_info_word|
  1136. per character. Each |char_info_word| contains six fields packed into
  1137. four bytes as follows.
  1138. \yskip\hang first byte: |width_index| (8 bits)\par
  1139. \hang second byte: |height_index| (4 bits) times 16, plus |depth_index|
  1140.   (4~bits)\par
  1141. \hang third byte: |italic_index| (6 bits) times 4, plus |tag|
  1142.   (2~bits)\par
  1143. \hang fourth byte: |remainder| (8 bits)\par
  1144. \yskip\noindent
  1145. The actual width of a character is |width[width_index]|, in design-size
  1146. units; this is a device for compressing information, since many characters
  1147. have the same width. Since it is quite common for many characters
  1148. to have the same height, depth, or italic correction, the \.{TFM} format
  1149. imposes a limit of 16 different heights, 16 different depths, and
  1150. 64 different italic corrections.
  1151. Incidentally, the relation |width[0]=height[0]=depth[0]=italic[0]=0|
  1152. should always hold, so that an index of zero implies a value of zero.
  1153. The |width_index| should never be zero unless the character does
  1154. not exist in the font, since a character is valid if and only if it lies
  1155. between |bc| and |ec| and has a nonzero |width_index|.
  1156. @ The |tag| field in a |char_info_word| has four values that explain how to
  1157. interpret the |remainder| field.
  1158. \yskip\hang|tag=0| (|no_tag|) means that |remainder| is unused.\par
  1159. \hang|tag=1| (|lig_tag|) means that this character has a ligature/kerning
  1160. program starting at |lig_kern[remainder]|.\par
  1161. \hang|tag=2| (|list_tag|) means that this character is part of a chain of
  1162. characters of ascending sizes, and not the largest in the chain.  The
  1163. |remainder| field gives the character code of the next larger character.\par
  1164. \hang|tag=3| (|ext_tag|) means that this character code represents an
  1165. extensible character, i.e., a character that is built up of smaller pieces
  1166. so that it can be made arbitrarily large. The pieces are specified in
  1167. |exten[remainder]|.\par
  1168. @d no_tag=0 {vanilla character}
  1169. @d lig_tag=1 {character has a ligature/kerning program}
  1170. @d list_tag=2 {character has a successor in a charlist}
  1171. @d ext_tag=3 {character is extensible}
  1172. @ The |lig_kern| array contains instructions in a simple programming language
  1173. that explains what to do for special letter pairs. Each word is a
  1174. |lig_kern_command| of four bytes.
  1175. \yskip\hang first byte: |skip_byte|, indicates that this is the final program
  1176.   step if the byte is 128 or more, otherwise the next step is obtained by
  1177.   skipping this number of intervening steps.\par
  1178. \hang second byte: |next_char|, ``if |next_char| follows the current character,
  1179.   then perform the operation and stop, otherwise continue.''\par
  1180. \hang third byte: |op_byte|, indicates a ligature step if less than~128,
  1181.   a kern step otherwise.\par
  1182. \hang fourth byte: |remainder|.\par
  1183. \yskip\noindent
  1184. In a kern step, an
  1185. additional space equal to |kern[256*(op_byte-128)+remainder]| is inserted
  1186. between the current character and |next_char|. This amount is
  1187. often negative, so that the characters are brought closer together
  1188. by kerning; but it might be positive.
  1189. There are eight kinds of ligature steps, having |op_byte| codes $4a+2b+c$ where
  1190. $0\le a\le b+c$ and $0\le b,c\le1$. The character whose code is
  1191. |remainder| is inserted between the current character and |next_char|;
  1192. then the current character is deleted if $b=0$, and |next_char| is
  1193. deleted if $c=0$; then we pass over $a$~characters to reach the next
  1194. current character (which may have a ligature/kerning program of its own).
  1195. Notice that if $a=0$ and $b=1$, the current character is unchanged; if
  1196. $a=b$ and $c=1$, the current character is changed but the next character is
  1197. unchanged.
  1198. If the very first instruction of the |lig_kern| array has |skip_byte=255|,
  1199. the |next_char| byte is the so-called right boundary character of this font;
  1200. the value of |next_char| need not lie between |bc| and~|ec|.
  1201. If the very last instruction of the |lig_kern| array has |skip_byte=255|,
  1202. there is a special ligature/kerning program for a left boundary character,
  1203. beginning at location |256*op_byte+remainder|.
  1204. The interpretation is that \TeX\ puts implicit boundary characters
  1205. before and after each consecutive string of characters from the same font.
  1206. These implicit characters do not appear in the output, but they can affect
  1207. ligatures and kerning.
  1208. If the very first instruction of a character's |lig_kern| program has
  1209. |skip_byte>128|, the program actually begins in location
  1210. |256*op_byte+remainder|. This feature allows access to large |lig_kern|
  1211. arrays, because the first instruction must otherwise
  1212. appear in a location |<=255|.
  1213. Any instruction with |skip_byte>128| in the |lig_kern| array must have
  1214. |256*op_byte+remainder<nl|. If such an instruction is encountered during
  1215. normal program execution, it denotes an unconditional halt; no ligature
  1216. command is performed.
  1217. @d stop_flag=128 {value indicating `\.{STOP}' in a lig/kern program}
  1218. @d kern_flag=128 {op code for a kern step}
  1219. @ Extensible characters are specified by an |extensible_recipe|,
  1220. which consists of four bytes called |top|, |mid|,
  1221. |bot|, and |rep| (in this order). These bytes are the character codes
  1222. of individual pieces used to build up a large symbol.
  1223. If |top|, |mid|, or |bot| are zero,
  1224. they are not present in the built-up result. For example, an extensible
  1225. vertical line is like an extensible bracket, except that the top and
  1226. bottom pieces are missing.
  1227. @ The final portion of a \.{TFM} file is the |param| array, which is another
  1228. sequence of |fix_word| values.
  1229. \yskip\hang|param[1]=@!slant| is the amount of italic slant, which is used
  1230. to help position accents. For example, |slant=.25| means that when you go
  1231. up one unit, you also go .25 units to the right. The |slant| is a pure
  1232. number; it's the only |fix_word| other than the design size itself that is
  1233. not scaled by the design size.
  1234. \hang|param[2]=space| is the normal spacing between words in text.
  1235. Note that character |" "| in the font need not have anything to do with
  1236. blank spaces.
  1237. \hang|param[3]=space_stretch| is the amount of glue stretching between words.
  1238. \hang|param[4]=space_shrink| is the amount of glue shrinking between words.
  1239. \hang|param[5]=x_height| is the height of letters for which accents don't
  1240. have to be raised or lowered.
  1241. \hang|param[6]=quad| is the size of one em in the font.
  1242. \hang|param[7]=extra_space| is the amount added to |param[2]| at the
  1243. ends of sentences.
  1244. When the character coding scheme is \.{TeX math symbols}, the font is
  1245. supposed to have 15 additional parameters called |num1|, |num2|, |num3|,
  1246. |denom1|, |denom2|, |sup1|, |sup2|, |sup3|, |sub1|, |sub2|, |supdrop|,
  1247. |subdrop|, |delim1|, |delim2|, and |axis_height|, respectively. When the
  1248. character coding scheme is \.{TeX math extension}, the font is supposed to
  1249. have six additional parameters called |default_rule_thickness| and
  1250. |big_op_spacing1| through |big_op_spacing5|.
  1251. @* Binary data and binary files.
  1252. We have seen that a \.{DVI}, \.{VF}, or \.{TFM} file is a sequence of
  1253. 8-bit bytes.
  1254. The bytes appear physically in what is called a `|packed file of 0..255|'
  1255. in \PASCAL\ lingo. One, two, three, or four consecutive bytes are often
  1256. interpreted as (signed or unsigned) integers.
  1257. We might as well define the corresponding data types.
  1258. @!@^system dependencies@>
  1259. @<Types...@>=
  1260. @!signed_byte=-@"80..@"7F; {signed one-byte quantity}
  1261. @!eight_bits=0..@"FF; {unsigned one-byte quantity}
  1262. @!signed_pair=-@"8000..@"7FFF; {signed two-byte quantity}
  1263. @!sixteen_bits=0..@"FFFF; {unsigned two-byte quantity}
  1264. @!signed_trio=-@"800000..@"7FFFFF; {signed three-byte quantity}
  1265. @!twentyfour_bits=0..@"FFFFFF; {unsigned three-byte quantity}
  1266. @!signed_quad=int_32; {signed four-byte quantity}
  1267. @ Packing is system dependent, and many \PASCAL\ systems fail to implement
  1268. such files in a sensible way (at least, from the viewpoint of producing
  1269. good production software).  For example, some systems treat all
  1270. byte-oriented files as text, looking for end-of-line marks and such
  1271. things. Therefore some system-dependent code is often needed to deal with
  1272. binary files, even though most of the program in this section of
  1273. \.{\title} is written in standard \PASCAL.
  1274. @^system dependencies@>
  1275. One common way to solve the problem is to consider files of |integer|
  1276. numbers, and to convert an integer in the range $-2^{31}\L x<2^{31}$ to
  1277. a sequence of four bytes $(a,b,c,d)$ using the following code, which
  1278. avoids the controversial integer division of negative numbers:
  1279. $$\vbox{\halign{#\hfil\cr
  1280. |if x>=0 then a:=x div @'100000000|\cr
  1281. |else begin x:=(x+@'10000000000)+@'10000000000; a:=x div @'100000000+128;|\cr
  1282. \quad|end|\cr
  1283. |x:=x mod @'100000000;|\cr
  1284. |b:=x div @'200000; x:=x mod @'200000;|\cr
  1285. |c:=x div @'400; d:=x mod @'400;|\cr}}$$
  1286. The four bytes are then kept in a buffer and output one by one. (On 36-bit
  1287. computers, an additional division by 16 is necessary at the beginning.
  1288. Another way to separate an integer into four bytes is to use/abuse
  1289. \PASCAL's variant records, storing an integer and retrieving bytes that are
  1290. packed in the same place; {\sl caveat implementor!\/}) It is also desirable
  1291. in some cases to read a hundred or so integers at a time, maintaining a
  1292. larger buffer.
  1293. @ We shall stick to simple \PASCAL\ in the standard version of this program,
  1294. for reasons of clarity, even if such simplicity is sometimes unrealistic.
  1295. @<Types...@>=
  1296. @!byte_file=packed file of eight_bits; {files that contain binary data}
  1297. @ Character packets extracted from \.{VF} files will be stored in a large
  1298. array |byte_mem|. Other packets of bytes, e.g., character packets
  1299. extracted from a \.{GF} or \.{PK} or \.{PXL} file could be stored in the
  1300. same way. A `|pckt_pointer|' variable, which signifies a packet,
  1301. is an index into another array |pckt_start|. The actual sequence of bytes
  1302. in the packet pointed to by |p| appears in positions |pckt_start[p]| to
  1303. |pckt_start[p+1]-1|, inclusive, in |byte_mem|.
  1304. Packets will also be used to store sequences of |ASCII_code|s; in this
  1305. respect the |byte_mem| array is very similar to \TeX's string pool and
  1306. part of the following code has, in fact, been copied more or less
  1307. verbatim from \TeX.
  1308. In other respects the packets resemble the identifiers used by
  1309. \.{TANGLE} and \.{WEAVE} (also stored in an array called |byte_mem|)
  1310. since there is, in general, at most one packet with a given contents;
  1311. thus part of the code below has been adapted from the corresponding code
  1312. in these programs.
  1313. Some \PASCAL\ compilers won't pack integers into a single byte unless the
  1314. integers lie in the range |-128..127|. To accommodate such systems we
  1315. access the array |byte_mem| only via macros that can easily be redefined.
  1316. @^system dependencies@>
  1317. @d bi(#) == # {convert from |eight_bits| to |packed_byte|}
  1318. @d bo(#) == # {convert from |packed_byte| to |eight_bits|}
  1319. @<Types...@>=
  1320. @!packed_byte = eight_bits; {elements of |byte_mem| array}
  1321. @!byte_pointer = 0..max_bytes; {an index into |byte_mem|}
  1322. @!pckt_pointer = 0..max_packets; {an index into |pckt_start|}
  1323. @ The global variable |byte_ptr| points to the first unused location in
  1324. |byte_mem| and |pckt_ptr| points to the first unused location in
  1325. |pckt_start|.
  1326. @<Globals...@>=
  1327. @!byte_mem: packed array [byte_pointer] of packed_byte; {bytes of packets}
  1328. @!pckt_start: array [pckt_pointer] of byte_pointer;
  1329.   {directory into |byte_mem|}
  1330. @!byte_ptr: byte_pointer;
  1331. @!pckt_ptr: pckt_pointer;
  1332. @ Several of the elementary operations with packets are performed using
  1333. \.{WEB} macros instead of \PASCAL\ procedures, because many of the
  1334. operations are done quite frequently and we want to avoid the
  1335. overhead of procedure calls. For example, here is
  1336. a simple macro that computes the length of a packet.
  1337. @.WEB@>
  1338. @d pckt_length(#)==(pckt_start[#+1]-pckt_start[#]) {the number of bytes
  1339.   in packet number \#}
  1340. @ Packets are created by appending bytes to |byte_mem|.
  1341. The |append_byte| macro, defined here, does not check to see if the
  1342. value of |byte_ptr| has gotten too high; this test is supposed to be
  1343. made before |append_byte| is used. There is also a |flush_byte|
  1344. macro, which erases the last byte appended.
  1345. To test if there is room to append |l| more bytes to |byte_mem|,
  1346. we shall write |pckt_room(l)|, which aborts \.{\title} and gives an
  1347. apologetic error message if there isn't enough room.
  1348. @d append_byte(#) == {put byte \# at the end of |byte_mem|}
  1349. begin byte_mem[byte_ptr]:=bi(#); incr(byte_ptr);
  1350. @d flush_byte == decr(byte_ptr) {forget the last byte in |byte_mem|}
  1351. @d pckt_room(#) == {make sure that |byte_mem| hasn't overflowed}
  1352.   begin if byte_ptr+# > max_bytes then overflow(str_bytes,max_bytes);
  1353.   end
  1354. @d append_one(#) ==
  1355. begin pckt_room(1); append_byte(#);
  1356. @ The length of the current packet is called |cur_pckt_length|:
  1357. @d cur_pckt_length == (byte_ptr - pckt_start[pckt_ptr])
  1358. @ Once a sequence of bytes has been appended to |byte_mem|, it
  1359. officially becomes a packet when the function |make_packet| is called.
  1360. This function returns as its value the identification number of either
  1361. an existing packet with the same contents or, if no such packet exists,
  1362. of the new packet. Thus two packets have the same contents if and only
  1363. if they have the same identification number. In order to locate the
  1364. packet with a given contents, or to find out that no such packet exists,
  1365. we need a hash table. The hash table is kept by the method of simple
  1366. chaining, where the heads of the individual lists appear in the |p_hash|
  1367. array. If |h| is a hash code, the hash table list starts at |p_hash[h]|
  1368. and proceeds through |p_link| pointers.
  1369. @d hash_size=353 {should be prime, must be |>256|}
  1370. @<Types...@>=
  1371. @!hash_code=0..hash_size;
  1372. @ @<Glob...@>=
  1373. @!p_link:array[pckt_pointer] of pckt_pointer; {hash table}
  1374. @!p_hash:array[hash_code] of pckt_pointer;
  1375. @ Initially |byte_mem| and all the hash lists are empty; |empty_packet|
  1376. is the empty packet.
  1377. @d empty_packet=0 {the empty packet}
  1378. @<Set init...@>=
  1379. pckt_ptr:=1; byte_ptr:=1;
  1380. pckt_start[0]:=1; pckt_start[1]:=1;
  1381. for h:=0 to hash_size-1 do p_hash[h]:=0;
  1382. @ @<Local variables for init...@>=
  1383. @!h:hash_code; {index into hash-head arrays}
  1384. @ Here now is the procedure for finding packets (and strings).
  1385. @p function make_packet:pckt_pointer;
  1386. label found;
  1387. var i,@!k:byte_pointer; {indices into |byte_mem|}
  1388. @!h:hash_code; {hash code}
  1389. @!s,@!l:byte_pointer; {start and length of the given packet}
  1390. @!p:pckt_pointer; {where the packet is being sought}
  1391. begin s:=pckt_start[pckt_ptr]; l:=byte_ptr-s; {compute start and length}
  1392. if l=0 then p:=empty_packet
  1393. else  begin @<Compute the packet hash code |h|@>;
  1394.   @<Compute the packet location |p|@>;
  1395.   if pckt_ptr=max_packets then overflow(str_packets,max_packets);
  1396.   incr(pckt_ptr); pckt_start[pckt_ptr]:=byte_ptr;
  1397.   end;
  1398. found:make_packet:=p;
  1399. @ A simple hash code is used: If the sequence of bytes is
  1400. $b_1b_2\ldots b_n$, its hash value will be
  1401. $$(2^{n-1}b_1+2^{n-2}b_2+\cdots+b_n)\,\bmod\,|hash_size|.$$
  1402. @<Compute the packet hash...@>=
  1403. h:=bo(byte_mem[s]); i:=s+1;
  1404. while i<byte_ptr do
  1405.   begin h:=(h+h+bo(byte_mem[i])) mod hash_size; incr(i);
  1406.   end
  1407. @ If the packet is new, it will be placed in position |p=pckt_ptr|,
  1408. otherwise |p| will point to its existing location.
  1409. @<Compute the packet location...@>=
  1410. p:=p_hash[h];
  1411. while p<>0 do
  1412.   begin if pckt_length(p)=l then
  1413.       @<Compare packet |p| with current packet, |goto found| if equal@>;
  1414.   p:=p_link[p];
  1415.   end;
  1416. p:=pckt_ptr; {the current packet is new}
  1417. p_link[p]:=p_hash[h]; p_hash[h]:=p {insert |p| at beginning of hash list}
  1418. @ @<Compare packet |p|...@>=
  1419. begin i:=s; k:=pckt_start[p];
  1420. while (i<byte_ptr)and(byte_mem[i]=byte_mem[k]) do
  1421.   begin incr(i); incr(k);
  1422.   end;
  1423. if i=byte_ptr then {all bytes agree}
  1424.   begin byte_ptr:=pckt_start[pckt_ptr]; goto found;
  1425.   end;
  1426. @ Some packets are initialized with predefined strings of |ASCII_code|s;
  1427. a few macros permit us to do the initialization with a compact program.
  1428. Since this initialization is done when |byte_mem| is still empty, and
  1429. since |byte_mem| is supposed to be large enough for all the predefined
  1430. strings, |pckt_room| is used only if we are debugging.
  1431. @d pid0(#)==#:=make_packet
  1432. @d pid1(#)==byte_mem[byte_ptr-1]:=bi(#); pid0
  1433. @d pid2(#)==byte_mem[byte_ptr-2]:=bi(#); pid1
  1434. @d pid3(#)==byte_mem[byte_ptr-3]:=bi(#); pid2
  1435. @d pid4(#)==byte_mem[byte_ptr-4]:=bi(#); pid3
  1436. @d pid5(#)==byte_mem[byte_ptr-5]:=bi(#); pid4
  1437. @d pid6(#)==byte_mem[byte_ptr-6]:=bi(#); pid5
  1438. @d pid7(#)==byte_mem[byte_ptr-7]:=bi(#); pid6
  1439. @d pid8(#)==byte_mem[byte_ptr-8]:=bi(#); pid7
  1440. @d pid9(#)==byte_mem[byte_ptr-9]:=bi(#); pid8
  1441. @d pid10(#)==byte_mem[byte_ptr-10]:=bi(#); pid9
  1442. @d pid_init(#)==
  1443.   @!debug pckt_room(#); @+ gubed @;
  1444.   Incr(byte_ptr)(#)
  1445. @d id1==pid_init(1); pid1
  1446. @d id2==pid_init(2); pid2
  1447. @d id3==pid_init(3); pid3
  1448. @d id4==pid_init(4); pid4
  1449. @d id5==pid_init(5); pid5
  1450. @d id6==pid_init(6); pid6
  1451. @d id7==pid_init(7); pid7
  1452. @d id8==pid_init(8); pid8
  1453. @d id9==pid_init(9); pid9
  1454. @d id10==pid_init(10); pid10
  1455. @ Here we initialize some strings used as argument of the |overflow| and
  1456. |confusion| procedures.
  1457. @<Initialize predefined strings@>=
  1458. id5("f")("o")("n")("t")("s")(str_fonts);
  1459. id5("c")("h")("a")("r")("s")(str_chars);
  1460. id6("w")("i")("d")("t")("h")("s")(str_widths);
  1461. id7("p")("a")("c")("k")("e")("t")("s")(str_packets);
  1462. id5("b")("y")("t")("e")("s")(str_bytes);
  1463. id9("r")("e")("c")("u")("r")("s")("i")("o")("n")(str_recursion);
  1464. id5("s")("t")("a")("c")("k")(str_stack);
  1465. id10("n")("a")("m")("e")("l")("e")("n")("g")("t")("h")(str_name_length);
  1466. @ @<Glob...@>=
  1467. @!str_fonts,@!str_chars,@!str_widths,@!str_packets,@!str_bytes,
  1468. @!str_recursion,@!str_stack,@!str_name_length:pckt_pointer;
  1469. @ Some packets, e.g., the preamble comments of \.{DVI} and \.{VF} files,
  1470. are needed only temporarily. In such cases |new_packet| is used to
  1471. create a packet (which might duplicate an existing packet) and
  1472. |flush_packet| is used to discard it; the calls to |new_packet| and
  1473. |flush_packet| must occur in balanced pairs, without any intervening
  1474. calls to |make_packet|.
  1475. @p function new_packet: pckt_pointer;
  1476. begin if pckt_ptr=max_packets then overflow(str_packets,max_packets);
  1477. new_packet:=pckt_ptr; incr(pckt_ptr); pckt_start[pckt_ptr]:=byte_ptr;
  1478. procedure flush_packet;
  1479. begin decr(pckt_ptr); byte_ptr:=pckt_start[pckt_ptr];
  1480. @ The |print_packet| procedure prints the contents of a packet; such a
  1481. packets should, of course, consists of a sequence of |ASCII_code|s.
  1482. @<Basic printing...@>=
  1483. procedure print_packet(p:pckt_pointer);
  1484. var k:byte_pointer;
  1485. begin for k:=pckt_start[p] to pckt_start[p+1]-1 do
  1486.   print(xchr[bo(byte_mem[k])]);
  1487. @ When we interpret a packet we will use two (global or local) variables:
  1488. |cur_loc| will point to the byte to be used next, and |cur_limit| will
  1489. point to the start of the next packet. The macro |pckt_extract| will be
  1490. used to extract one byte; it should, however, never be used with
  1491. |cur_loc>=cur_limit|.
  1492. @d pckt_extract(#) ==
  1493. @!debug if cur_loc>=cur_limit then confusion(str_packets) @+ else @/
  1494. gubed @;
  1495.   begin #:=bo(byte_mem[cur_loc]); incr(cur_loc); @+ end
  1496. @<Globals...@>=
  1497. @!cur_pckt: pckt_pointer; {the current packet}
  1498. @!cur_loc: byte_pointer; {current location in a packet}
  1499. @!cur_limit: byte_pointer; {start of next packet}
  1500. @ We will need routines to extract one, two, three, or four bytes from
  1501. |byte_mem|, from the \.{DVI} file, or from a \.{VF} file and assemble
  1502. them into (signed or unsigned) integers and these routines should be
  1503. optimized for efficiency. Here we define \.{WEB} macros to be used for
  1504. the body of these routines; thus the changes for system dependent
  1505. optimization have to be applied only once.
  1506. @^system dependencies@>
  1507. @^optimization@>
  1508. In addition we demonstrates how these macros can be used to define
  1509. functions that extract one, two, three, or four bytes from a character
  1510. packet and assemble them into signed or unsigned integers (assuming that
  1511. |cur_loc| and |cur_limit| are initialized suitably).
  1512. @d begin_byte(#) ==
  1513. var a:eight_bits;
  1514. begin #(a)
  1515. @d comp_sbyte(#) == if a<128 then #:=a @+ else #:=a-256
  1516. @d comp_ubyte(#) == #:=a
  1517. @f begin_byte == begin
  1518. @p function pckt_sbyte:int_8; {returns the next byte, signed}
  1519. @!begin_byte(pckt_extract); comp_sbyte(pckt_sbyte);
  1520. function pckt_ubyte:int_8u; {returns the next byte, unsigned}
  1521. @!begin_byte(pckt_extract); comp_ubyte(pckt_ubyte);
  1522. @ @d begin_pair(#) ==
  1523. var a,@!b:eight_bits;
  1524. begin #(a); #(b)
  1525. @d comp_spair(#) == if a<128 then #:=a*256+b @+ else #:=(a-256)*256+b
  1526. @d comp_upair(#) == #:=a*256+b
  1527. @f begin_pair == begin
  1528. @p function pckt_spair:int_16; {returns the next two bytes, signed}
  1529. @!begin_pair(pckt_extract); comp_spair(pckt_spair);
  1530. function pckt_upair:int_16u; {returns the next two bytes, unsigned}
  1531. @!begin_pair(pckt_extract); comp_upair(pckt_upair);
  1532. @ @d begin_trio(#) ==
  1533. var a,@!b,@!c:eight_bits;
  1534. begin #(a); #(b); #(c)
  1535. @d comp_strio(#) ==
  1536. if a<128 then #:=(a*256+b)*256+c @+ else #:=((a-256)*256+b)*256+c
  1537. @d comp_utrio(#) == #:=(a*256+b)*256+c
  1538. @f begin_trio == begin
  1539. @p function pckt_strio:int_24; {returns the next three bytes, signed}
  1540. @!begin_trio(pckt_extract); comp_strio(pckt_strio);
  1541. function pckt_utrio:int_24u; {returns the next three bytes, unsigned}
  1542. @!begin_trio(pckt_extract); comp_utrio(pckt_utrio);
  1543. @ @d begin_quad(#) ==
  1544. var a,@!b,@!c,@!d:eight_bits;
  1545. begin #(a); #(b); #(c); #(d)
  1546. @d comp_squad(#) ==
  1547. if a<128 then #:=((a*256+b)*256+c)*256+d
  1548. else #:=(((a-256)*256+b)*256+c)*256+d
  1549. @f begin_quad == begin
  1550. @p function pckt_squad:int_32; {returns the next four bytes, signed}
  1551. @!begin_quad(pckt_extract); comp_squad(pckt_squad);
  1552. @ A similar set of routines is needed for the inverse task of
  1553. decomposing a \.{DVI} command into a sequence of bytes to be appended
  1554. to |byte_mem| or, in the case of \.{DVIcopy}, to be written to the
  1555. output file. Again we define \.{WEB} macros to be used for the body
  1556. of these routines; thus the changes for system dependent optimization
  1557. have to be applied only once.
  1558. @^system dependencies@>
  1559. @^optimization@>
  1560. First, the |pckt_four| procedure outputs four bytes in two's complement
  1561. notation, without risking arithmetic overflow.
  1562. @d begin_four == begin
  1563. @d comp_four(#) ==
  1564. if x>=0 then #(x div @"1000000)
  1565. else  begin Incr(x)(@"40000000); Incr(x)(@"40000000);
  1566.   #((x div @"1000000) + 128);
  1567.   end;
  1568. x:=x mod @"1000000; #(x div @"10000);
  1569. x:=x mod @"10000; #(x div @"100);
  1570. #(x mod @"100)
  1571. @f begin_four == begin
  1572. @p procedure pckt_four(@!x:int_32); {output four bytes}
  1573. @!begin_four; pckt_room(4); comp_four(append_byte);
  1574. @ Next, the |pckt_char| procedure outputs a |set_char| or \\{set} command
  1575. or, if |upd=false|, a |put| command.
  1576. @d begin_char ==
  1577. var o:eight_bits; {|set1| or |put1|}
  1578. begin
  1579. @d comp_char(#) ==
  1580. if (not upd)or(res>127)or(ext<>0) then
  1581.   begin o:=dvi_char_cmd[upd]; {|set1| or |put1|}
  1582.   if ext<0 then Incr(ext)(@"1000000);
  1583.   if ext=0 then #(o) @+ else @;
  1584.     begin if ext<@"100 then #(o+1) @+ else @;
  1585.       begin if ext<@"10000 then #(o+2) @+ else @;
  1586.         begin #(o+3); #(ext div @"10000); ext:=ext mod @"10000;
  1587.         end;
  1588.       #(ext div @"100); ext:=ext mod @"100;
  1589.       end;
  1590.     #(ext);
  1591.     end;
  1592.   end;
  1593. #(res)
  1594. @f begin_char == begin
  1595. @p procedure pckt_char(@!upd:boolean;@!ext:int_32;@!res:eight_bits);
  1596.   {output \\{set} or |put|}
  1597. @!begin_char; pckt_room(5); comp_char(append_byte);
  1598. @ Then, the |pckt_unsigned| procedure outputs a |fnt| or |xxx|
  1599. command with its first parameter (normally unsigned); a |fnt| command
  1600. is converted into |fnt_num| whenever this is possible.
  1601. @d begin_unsigned == begin
  1602. @d comp_unsigned(#) ==
  1603. if (x<@"100)and(x>=0) then
  1604.   if (o=fnt1)and(x<64) then Incr(x)(fnt_num_0) @+ else #(o)
  1605.   begin if (x<@"10000)and(x>=0) then #(o+1) @+ else @;
  1606.     begin if (x<@"1000000)and(x>=0) then #(o+2) @+ else @;
  1607.       begin #(o+3);
  1608.       if x>=0 then #(x div @"1000000)
  1609.       else  begin Incr(x)(@"40000000); Incr(x)(@"40000000);
  1610.         #((x div @"1000000) + 128);
  1611.         end;
  1612.       x:=x mod @"1000000;
  1613.       end;
  1614.     #(x div @"10000); x:=x mod @"10000;
  1615.     end;
  1616.   #(x div @"100); x:=x mod @"100;
  1617.   end;
  1618. @f begin_unsigned == begin
  1619. @p procedure pckt_unsigned(@!o:eight_bits;@!x:int_32);
  1620.   {output |fnt_num|, |fnt|, or |xxx|}
  1621. @!begin_unsigned; pckt_room(5); comp_unsigned(append_byte);
  1622. @ Finally, the |pckt_signed| procedure outputs a movement (|right|, |w|,
  1623. |x|, |down|, |y|, or |z|) command with its (signed) parameter.
  1624. @d begin_signed ==
  1625. var xx:int_31; {`absolute value' of |x|}
  1626. begin
  1627. @d comp_signed(#) ==
  1628. if x>=0 then xx:=x @+ else xx:=-(x+1);
  1629. if xx<@"80 then
  1630.   begin #(o); @+ if x<0 then Incr(x)(@"100); @+ end
  1631. else  begin if xx<@"8000 then
  1632.     begin #(o+1); @+ if x<0 then Incr(x)(@"10000); @+ end
  1633.   else  begin if xx<@"800000 then
  1634.       begin #(o+2); @+ if x<0 then Incr(x)(@"1000000); @+ end
  1635.     else  begin #(o+3);
  1636.       if x>=0 then #(x div @"1000000)
  1637.       else  begin x:=@"7FFFFF-xx; #((x div @"1000000) + 128); @+ end;
  1638.       x:=x mod @"1000000;
  1639.       end;
  1640.     #(x div @"10000); x:=x mod @"10000;
  1641.     end;
  1642.   #(x div @"100); x:=x mod @"100;
  1643.   end;
  1644. @f begin_signed == begin
  1645. @p procedure pckt_signed(@!o:eight_bits;@!x:int_32);
  1646.   {output |right|, |w|, |x|, |down|, |y|, or |z|}
  1647. @!begin_signed; pckt_room(5); comp_signed(append_byte);
  1648. @ The character code of each character to be typset will be decomposed
  1649. into a residue |0<=char_res<256| and extension:
  1650. |char_code=char_res+256*char_ext|; the \.{TFM} widths as well as the
  1651. pixel widths for a given resolution are the same for all characters in a
  1652. font with the same residue. A \.{VF} or \.{GF} or \.{PK} file may
  1653. contain information for several characters with the same residue but
  1654. with different extension; all except the first of the corresponding
  1655. packets in |byte_mem| will contain a pointer to the previous one and the
  1656. table of packet pointers for all legal characters in the font will point
  1657. to the last such packet.
  1658. A character packet in |byte_mem| starts with a flag byte
  1659. $$\hbox{|flag=@"40*ext_flag+@"20*chain_flag+type_flag|}$$
  1660. with |0<=ext_flag<=3|, |0<=chain_flag<=1|, |0<=type_flag<=@"1F|,
  1661. followed by |ext_flag| bytes with the character extension for this
  1662. packet and, if |chain_flag=1|, by a two byte packet pointer to the
  1663. previous packet for the same font and character residue. The actual
  1664. character packet follows after these header bytes and the
  1665. interpretation of the |type_flag| depends on whether this is a \.{VF} or
  1666. \.{GF} or \.{PK} packet.
  1667. The empty packet is interpreted as a special case of a packet with
  1668. |flag=0|.
  1669. @d ext_flag=@"40
  1670. @d chain_flag=@"20
  1671. @<Types...@>=
  1672. @!type_flag=0..chain_flag-1; {the range of values for the |type_flag|}
  1673. @ Given an extension, the pointer to the previous packet (if any), and
  1674. a type, the |start_packet| procedure stores these hader bytes in
  1675. |byte_mem|.
  1676. @d invalid_packet==max_packets {used when there is no packet}
  1677. @p procedure start_packet(@!ext:int_32;@!r:pckt_pointer;@!t:eight_bits);
  1678. label continue;
  1679. var p,@!q:pckt_pointer; {current and next packet}
  1680. @!f:eight_bits; {a flag byte}
  1681. @!e:int_24; {extension for a packet}
  1682. @!cur_loc: byte_pointer; {current location in a packet}
  1683. @!cur_limit: byte_pointer; {start of next packet}
  1684. begin
  1685. @!debug if byte_ptr<>pckt_start[pckt_ptr] then confusion(str_packets);
  1686. gubed @;@/
  1687. pckt_room(6);
  1688. if r=invalid_packet then f:=t
  1689. else  begin q:=r; @<Locate character packet for extension |ext|@>;
  1690.   if e=ext then dup_warning(ext);
  1691.   f:=t+chain_flag
  1692.   end;
  1693. if ext<0 then Incr(ext)(@"1000000);
  1694. if ext=0 then append_byte(f) @+ else @;
  1695.   begin if ext<@"100 then append_byte(f+ext_flag) @+ else @;
  1696.     begin if ext<@"10000 then append_byte(f+ext_flag+ext_flag) @+ else @;
  1697.       begin append_byte(f+ext_flag+ext_flag+ext_flag);
  1698.       append_byte(ext div @"10000); ext:=ext mod @"10000;
  1699.       end;
  1700.     append_byte(ext div @"100); ext:=ext mod @"100;
  1701.     end;
  1702.   append_byte(ext);
  1703.   end;
  1704. if r<>invalid_packet then
  1705.   begin append_byte(r div @"100); append_byte(r mod @"100);
  1706.   end;
  1707. @ Given the pointer |q| to a non-empty chain of character packets, the
  1708. |find_packet| function locates the packet for extension |ext|; if no
  1709. such packet is found, a warning message is given and the first packet
  1710. (the last one in the chain) is used. The functions sets |cur_pckt|,
  1711. |cur_loc|, and |cur_limit| and returns the |type_flag| of the packet.
  1712. The |find_packet| function must not be called for an empty chain.
  1713. @p function find_packet(@!ext:int_24;@!q:pckt_pointer):type_flag;
  1714. label continue;
  1715. var p:pckt_pointer; {current packet}
  1716. @!f:eight_bits; {a flag byte}
  1717. @!e:int_24; {extension for a packet}
  1718. begin
  1719. @!debug if q=invalid_packet then confusion(str_packets);
  1720. gubed @;@/
  1721. @<Locate character packet for extension |ext|@>;
  1722. if e<>ext then subst_warning(e,ext);
  1723. cur_pckt:=p;
  1724. find_packet:=f;
  1725. @ The following code follows a non-empty chain of packets until either a
  1726. packet for the desired extension is found (|e=ext|) or the chain has
  1727. ended.
  1728. @<Locate character packet for extension |ext|@>=
  1729. continue: p:=q; cur_loc:=pckt_start[p]; cur_limit:=pckt_start[p+1];
  1730. if p=empty_packet then
  1731.   begin e:=0; f:=0;
  1732.   end
  1733. else begin  pckt_extract(f);
  1734.   case (f div ext_flag) of
  1735.   0: e:=0;
  1736.   1: e:=pckt_ubyte;
  1737.   2: e:=pckt_upair;
  1738.   3: e:=pckt_strio;
  1739.   end;
  1740.   if (f mod ext_flag)>=chain_flag then
  1741.     begin q:=pckt_upair; if e<>ext then goto continue;
  1742.     end;
  1743.   f:=f mod chain_flag;
  1744. @ The |hex_packet| procedure prints the contents of a packet in
  1745. hexadecimal form.
  1746. @<Basic printing...@>=
  1747. @!debug procedure hex_packet(@!p:pckt_pointer); {prints a packet in hex}
  1748. var j,@!k,@!l:byte_pointer; {indices into |byte_mem|}
  1749. @!d:int_8u;
  1750. begin j:=pckt_start[p]-1; k:=pckt_start[p+1]-1;
  1751. print_ln(' packet=',p:1,' start=',j+1:1,' length=',k-j:1);
  1752. for l:=j+1 to k do
  1753.   begin d:=(bo(byte_mem[l])) div 16;
  1754.   if d<10 then print(xchr[d+"0"]) @+ else print(xchr[d-10+"A"]);
  1755.   d:=(bo(byte_mem[l])) mod 16;
  1756.   if d<10 then print(xchr[d+"0"]) @+ else print(xchr[d-10+"A"]);
  1757.   if (l=k)or(((l-j) mod 16)=0) then print_ln(' ')
  1758.   else if ((l-j) mod 4)=0 then print('  ')
  1759.   else print(' ');
  1760.   end;
  1761. gubed
  1762. @* File names.
  1763. The structure of file names is different for different systems; therefore
  1764. this part of the program will, in most cases, require system dependent
  1765. modifications. Here we assume that a file name consists of three parts:
  1766. an area or directory specifying where the file can be found, a name
  1767. proper and an extension; \.{\title} assumes that these three parts appear
  1768. in order stated above but this need not be true in all cases.
  1769. The font names extracted from \.{DVI} and \.{VF} files consist of an area
  1770. part and a name proper; these are stored as packets consisting of the
  1771. length of the area part followed by the area and the name proper.
  1772. When we print an external font name we simple print the area and the name
  1773. contained in the `file name packet' without delimiter between them.
  1774. This may need to be modified for some systems.
  1775. @^system dependencies@>
  1776. @<Basic printing...@>=
  1777. procedure print_font(@!f:font_number);
  1778. var p:pckt_pointer; {the font name packet}
  1779. @!k:byte_pointer; {index into |byte_mem|}
  1780. @!m:int_31; {font magnification}
  1781. begin print(' = '); p:=font_name(f);
  1782. for k:=pckt_start[p]+1 to pckt_start[p+1]-1 do
  1783.   print(xchr[bo(byte_mem[k])]);
  1784. m:=round((font_scaled(f)/font_design(f))*dvi_mag);
  1785. if m<>1000 then print(' scaled ',m:1);
  1786. @ Before a font file can be opened for input we must build a string
  1787. with its external name.
  1788. @<Glob...@>=
  1789. @!cur_name:packed array[1..name_length] of char; {external name,
  1790.   with no lower case letters}
  1791. @ For \.{TFM} and \.{VF} files we just append the apropriate extension
  1792. to the file name packet; in addition a system dependent area part
  1793. (usually different for \.{TFM} anf \.{VF} files) is prepended if
  1794. the file name packet contains no area part. For other font files (e.g.,
  1795. \.{GF} or \.{PK}) the extension or area part will in most cases depend on
  1796. the resolution of the output device (corrected for font magnification).
  1797. The |make_name| procedure used to build the external file name has three
  1798. parameters: the packets with the font name and the extension, and the
  1799. length of the default area which must be copied to |cur_name| before
  1800. |make_name| is called.
  1801. @^system dependencies@>
  1802. @p procedure make_name(@!n,@!e:pckt_pointer;@!r:int_15);
  1803. var b:eight_bits; {a byte extracted from |byte_mem|}
  1804. @!cur_loc,@!cur_limit:byte_pointer; {indices into |byte_mem|}
  1805. begin cur_loc:=pckt_start[n]; cur_limit:=pckt_start[n+1];
  1806. pckt_extract(b); {length of area part}
  1807. if b>0 then r:=0;
  1808. if r+(cur_limit-cur_loc)+pckt_length(e)>name_length then
  1809.   overflow(str_name_length,name_length);
  1810. while cur_loc<cur_limit do
  1811.   begin pckt_extract(b);
  1812.   if (b>="a")and(b<="z") then b:=b-@'40; {convert to upper case}
  1813.   incr(r); cur_name[r]:=xchr[b];
  1814.   end;
  1815. cur_loc:=pckt_start[e]; cur_limit:=pckt_start[e+1];
  1816. while cur_loc<cur_limit do
  1817.   begin pckt_extract(b);
  1818.   incr(r); cur_name[r]:=xchr[b];
  1819.   end;
  1820. while r<name_length do  begin incr(r); cur_name[r]:=' '; @+ end;
  1821. @* Defining fonts.
  1822. \.{DVI} file format does not include information about character widths, since
  1823. that would tend to make the files a lot longer. But a program that reads
  1824. a \.{DVI} file is supposed to know the widths of the characters that appear
  1825. in \\{set\_char} commands. Therefore \.{\title} looks at the font metric
  1826. (\.{TFM}) files for the fonts that are involved.
  1827. @.TFM {\rm files}@>
  1828. The character-width data appears also in other files (e.g., in \.{VF} files
  1829. or in \.{GF} files that specify bit patterns for digitized characters);
  1830. thus, it is usually possible for \.{DVI} reading programs to get by with
  1831. accessing only one file per font. For \.{VF} reading programs there is,
  1832. however, a problem: (1)~when reading the character packets from a
  1833. \.{VF} file the \.{TFM} width for its local fonts should be known in
  1834. order to analyze and optimize the packets (e.g., determine if a packet
  1835. must indeed be enclosed with |push| and |pop| as implied by the \.{VF}
  1836. format); and (2)~ in order to avoid infinite recursion such programs
  1837. must not try to read a \.{VF} file for a font before a character from
  1838. that font is actually used. Thus \.{\title} reads the \.{TFM} file
  1839. whenever a new font is encountered and delays the decision whether this
  1840. is a virtual font or not.
  1841. @ For the moment, we need to know only two things about a
  1842. given character |c| in a given font |f|: (1)~Is |c| a legal character
  1843. in~|f|? (2)~If so, what is the width of |c|? We also need to know the
  1844. symbolic name of each font, so it can be printed out, and we need to know
  1845. the approximate size of inter-word spaces in each font.
  1846. The answers to these questions appear implicitly in the data structures
  1847. defined in the following sections.
  1848. @ Quite often a particular width value is shared by several characters in
  1849. a font or even by characters from different fonts; the later will
  1850. probably occur in particular for virtual fonts and the local fonts used
  1851. by them. Thus the array |widths| is used to store all different \.{TFM}
  1852. width values of all legal characters in all fonts; a variable of type
  1853. |width_pointer| is an index into |widths| or is zero if a characters does
  1854. not exist. If the output is for a real typesetting device the |pix_widths|
  1855. array contains the same width values converted to (horizontal) pixels.
  1856. In order to locate a given width value we use again a hash
  1857. table with simple chaining; this time the heads of the individual lists
  1858. appear in the |w_hash| array and the lists proceed through |w_link|
  1859. pointers.
  1860. @<Types...@>=
  1861. @!width_pointer=0..max_widths; {an index into |widths|}
  1862. @!device
  1863. @!pix_value=-@"8000..@"7FFF; {a pixel coordinate or displacement;
  1864.   this range may not suffice for high resolution output devices}
  1865. ecived
  1866. @ @<Glob...@>=
  1867. @!widths:array[width_pointer] of int_32; {the different width values}
  1868. @!device
  1869. @!pix_widths:array[width_pointer] of pix_value; {the widths in pixels}
  1870. ecived @; @/
  1871. @!w_link:array[width_pointer] of width_pointer; {hash table}
  1872. @!w_hash:array[hash_code] of width_pointer;
  1873. @!n_widths:width_pointer; {first unoccupied position in |widths|}
  1874. @ Initially the |widths| array and all the hash lists are empty, except
  1875. for one entry: the width value zero; in addition we set |widths[0]:0|.
  1876. @d invalid_width=1 {width pointer for invalid characters}
  1877. @d zero_width=1 {a width pointer to the value zero}
  1878. @<Set init...@>=
  1879. w_hash[0]:=1; w_link[1]:=0; widths[0]:=0; widths[1]:=0; n_widths:=2;
  1880. @!device pix_widths[0]:=0; pix_widths[1]:=0; @+ ecived @;
  1881. for h:=1 to hash_size-1 do w_hash[h]:=0;
  1882. @ The function |make_width| returns an index into |widths| and, if
  1883. necessary, adds a new width value; thus two characters will have the
  1884. same |width_pointer| if and only if their widths agree.
  1885. @d h_pixel_round(#)==round(h_conv*(#))
  1886. @d v_pixel_round(#)==round(v_conv*(#))
  1887. @^system dependencies@>
  1888. @p function make_width(@!w:int_32):width_pointer;
  1889. label found;
  1890. var h:hash_code; {hash code}
  1891. @!p:width_pointer; {where the identifier is being sought}
  1892. @!x:int_16; {intermediate value}
  1893. begin widths[n_widths]:=w;
  1894. @<Compute the width hash code |h|@>;
  1895. @<Compute the width location |p|, |goto| found unless the value is new@>;
  1896. if n_widths=max_widths then overflow(str_widths,max_widths);
  1897. incr(n_widths);
  1898. @!device pix_widths[p]:=h_pixel_round(w); @+ ecived @;
  1899. found:make_width:=p;
  1900. @ A simple hash code is used: If the width value consists of the four
  1901. bytes $b_0b_1b_2b_3$, its hash value will be
  1902. $$(8*b_0+4*b_1+2*b_2+b_3)\,\bmod\,|hash_size|.$$
  1903. @<Compute the width hash...@>=
  1904. if w>=0 then x:=w div @"1000000
  1905. else  begin w:=w+@"40000000; w:=w+@"40000000; x:=(w div @"1000000)+@"80;
  1906.   end;
  1907. w:=w mod @"1000000; x:=x+x+(w div @"10000);
  1908. w:=w mod @"10000; x:=x+x+(w div @"100);
  1909. h:=(x+x+(w mod @"100)) mod hash_size
  1910. @ If the width is new, it has been placed into position |p=n_widths|,
  1911. otherwise |p| will point to its existing location.
  1912. @<Compute the width location...@>=
  1913. p:=w_hash[h];
  1914. while p<>0 do
  1915.   begin if widths[p]=widths[n_widths] then goto found;
  1916.   p:=w_link[p];
  1917.   end;
  1918. p:=n_widths; {the current width is new}
  1919. w_link[p]:=w_hash[h]; w_hash[h]:=p {insert |p| at beginning of hash list}
  1920. @ The |char_widths| array is used to store the |width_pointer|s for all
  1921. different characters among all fonts. For a real typesetting device the
  1922. |char_pixels| array is used to store the horizontal character escapements:
  1923. Initially we use the |pix_widths| values, but these will be replaced by
  1924. the character escapements specified in a \.{PK} or \.{GF} file;
  1925. these values may differ by a small amount.
  1926. The |char_packets| array is used to store the |pckt_pointer|s for all
  1927. different characters among all virtual fonts; pointers to packets from
  1928. other font files, e.g., from \.{PK} files, can be stored in the same way.
  1929. @<Types...@>=
  1930. @!char_offset=-255..max_chars; {|char_pointer| offset for a font}
  1931. @!char_pointer=0..max_chars; {index into |char_widths| or similar arrays}
  1932. @ @<Glob...@>=
  1933. @!char_widths:array[char_pointer] of width_pointer; {width pointers}
  1934. @!device
  1935. @!char_pixels:array[char_pointer] of pix_value; {character escapements}
  1936. ecived @; @/
  1937. @!char_packets:array[char_pointer] of pckt_pointer; {packet pointers}
  1938. @!n_chars:char_pointer; {first unused position in |char_widths|}
  1939. @!n_packets:char_pointer; {first unused position in |char_packets|}
  1940. @ @<Set init...@>=
  1941. n_chars:=0; n_packets:=0;
  1942. @ The current number of known fonts is |nf|; each known font has an
  1943. internal number |f|, where |0<=f<nf|. For the moment we need for each
  1944. known font: |font_check|, |font_scaled|, |font_design|, |font_space|,
  1945. |font_name|, |font_bc|, |font_ec|, |font_chars|, and |font_type|.
  1946. Here |font_scaled|, |font_design|, and |font_space| are measured in
  1947. \.{DVI} units and |font_chars| is of type |char_offset|:
  1948. the width pointer for character~|c| of the font is stored in
  1949. |char_widths[char_offset+c]| (for |font_bc<=c<=font_ec|).
  1950. Lateron we will need additional information depending on the font type:
  1951. \.{VF} or real (\.{GF}, \.{PK}, or \.{PXL}).
  1952. These data are stored in an array of record structures with a variant
  1953. part depending on the font type and defined elsewhere in this program.
  1954. @^font types@>
  1955. @<Types...@>=
  1956. @!f_type=new_font_type..max_font_type; {type of a font}
  1957. @!font_number=0..max_fonts;
  1958. @ @<Glob...@>=
  1959. @!font_data:array[font_number] of font_record; {all data for all fonts}
  1960. @!nf:font_number;
  1961. @ We use \.{WEB} macros to access the various fields. We will say, e.g.,
  1962. |font_name(f)| for the name field of font~|f|, and |font_width(f)(c)|
  1963. for the width pointer of character~|c| in font~|f| (this character
  1964. exists provided |font_bc(f)<=c<=font_ec(f)| and |font_width(f)(c)>0|).
  1965. The actual width of character~|c| in font~|f| is stored in
  1966. |widths[font_width(f)(c)]|; the horizontal escapement is given by
  1967. |font_pixel(f)(c)|.
  1968. @d font_check(#)==font_data[#].check_field {checksum}
  1969. @d font_scaled(#)==font_data[#].scaled_field {scaled or `at' size}
  1970. @d font_design(#)==font_data[#].design_field {design size}
  1971. @d font_space(#)==font_data[#].space_field {boundary between ``small''
  1972.   and ``large'' spaces}
  1973. @d font_name(#)==font_data[#].name_field {area plus name packet}
  1974. @d font_bc(#)==font_data[#].bc_field {first character}
  1975. @d font_ec(#)==font_data[#].ec_field {last character}
  1976. @d font_chars(#)==font_data[#].chars_field {character width offset}
  1977. @d font_type(#)==font_data[#].type_field {type of this font}
  1978. @d font_width_end(#)==#]
  1979. @d font_width(#)==char_widths[font_chars(#)+font_width_end
  1980. @d font_pixel(#)==char_pixels[font_chars(#)+font_width_end
  1981. @<Types...@>=
  1982. @!font_record=packed record@; {all data for one font}
  1983.   @!check_field:int_32; {checksum}
  1984.   @!scaled_field:int_31; {scaled size}
  1985.   @!design_field:int_31; {design size}
  1986.   @!device
  1987.   @!space_field:int_32; {boundary between ``small'' and ``large'' spaces}
  1988.   ecived @;
  1989.   @!name_field:pckt_pointer; {pointer to area plus name packet}
  1990.   @!bc_field:eight_bits; {first character}
  1991.   @!ec_field:eight_bits; {last character}
  1992.   @!chars_field:char_offset; {character width offset}
  1993.   @!type_field:f_type; {type of font}
  1994.   case f_type of@;@/
  1995.   @<Cases for |font_record|@>@;
  1996.   end;
  1997. @ Here we define the additional |font_data| fields required for a new
  1998. font which has been defined but not yet been used.
  1999. In order to simplify the \.{web2c} translation the fields in the variant
  2000. for |f_type=new_font_type| are accessed through the \.{WEB} macro
  2001. |new_font_data|.
  2002. @^font types@>@.web2c@>
  2003. Actually we need no fields for this record variant (at least for the
  2004. moment), but standard \PASCAL\ requires us to declare a field list for
  2005. each possible tag type value.
  2006. @d new_font_data(#)==font_data[#] {access |new_font_type| variant fields}
  2007. @<Cases for |font_record|@>=
  2008. new_font_type:
  2009. (); {there are no fields}
  2010. @ @d invalid_font==max_fonts {used when there is no valid font}
  2011. @<Set init...@>=
  2012. @!device font_space(invalid_font):=0; @+ ecived @;
  2013. nf:=0;
  2014. @ The |make_char_packets| function allocates and initializes packet
  2015. pointers in the |char_packets| array for all characters in a font and
  2016. returns the character packet offset.
  2017. @p function make_char_packets(@!f:font_number):char_offset;
  2018. var p:char_offset; {the character offset value to be returned}
  2019. @!k:char_pointer; {index into |char_packets|}
  2020. begin p:=n_packets-font_bc(f);
  2021. if font_ec(f)>=max_chars-p then overflow(str_chars,max_chars);
  2022. n_packets:=p+font_ec(f)+1;
  2023. for k:=p+font_bc(f) to n_packets-1 do char_packets[k]:=invalid_packet;
  2024. make_char_packets:=p;
  2025. @ In order to read \.{TFM} files the program uses the binary file
  2026. variable |tfm_file|.
  2027. @<Glob...@>=
  2028. @!tfm_file:byte_file; {a \.{TFM} file}
  2029. @!tfm_ext:pckt_pointer; {extension for \.{TFM} files}
  2030. @!cur_tfm:font_number; {font number of current \.{TFM} file}
  2031. @ @<Initialize predefined strings@>=
  2032. id4(".")("T")("F")("M")(tfm_ext); {file name extension for \.{TFM} files}
  2033. @ If a \.{TFM} file is badly malformed, we say |bad_tfm|; this procedure
  2034. gives an error message which refers the user to \.{TFtoPL} and \.{PLtoTF},
  2035. and terminates \.{\title}.
  2036. @<Error handling...@>=
  2037. procedure bad_tfm;
  2038. begin print_ln(' ');
  2039. print('Bad TFM file'); print_font(cur_tfm); print_ln('!');
  2040. @.Bad TFM file@>
  2041. abort('Use TFtoPL/PLtoTF to diagnose and correct the problem');
  2042. @.Use TFtoPL/PLtoTF@>
  2043. @ To prepare |tfm_file| for input we |reset| it.
  2044. @<TFM: Open |tfm_file|@>=
  2045. reset(tfm_file,cur_name);
  2046. @^system dependencies@>
  2047. if eof(tfm_file) then
  2048.   abort('---not loaded, TFM file can''t be opened!');
  2049. @.TFM file can\'t be opened@>
  2050. cur_tfm:=f {in case |bad_tfm| is called}
  2051. @ For some operating systems it may be necessary to close |tfm_file|.
  2052. @<TFM: Close |tfm_file|@>=
  2053. @ It turns out to be convenient to read four bytes at a time, when we
  2054. are inputting from \.{TFM} files. The input goes into global variables
  2055. |tfm_b0|, |tfm_b1|, |tfm_b2|, and |tfm_b3|, with |tfm_b0| getting the
  2056. first byte and |tfm_b3| the fourth.
  2057. @<Glob...@>=
  2058. @!tfm_b0,@!tfm_b1,@!tfm_b2,@!tfm_b3: eight_bits; {four bytes input at once}
  2059. @ Reading a \.{TFM} file should be done as efficient as possible for a
  2060. particular system; on many systems this means that a large number of
  2061. bytes from |tfm_file| is read into a buffer and will then be extracted
  2062. from that buffer. In order to simplify such system dependent changes
  2063. we use the \.{WEB} macro |tfm_byte| to extract the next \.{TFM} byte;
  2064. this macro and |eof(tfm_file)| are used only in the |read_tfm_word|
  2065. procedure which sets |tfm_b0| through |tfm_b3| to the next four bytes
  2066. in the current \.{TFM} file. Here we give simple minded definitions in
  2067. terms of standard \PASCAL.
  2068. @^system dependencies@>
  2069. @^optimization@>
  2070. @d tfm_byte(#)==read(tfm_file,#) {read next \.{TFM} byte}
  2071. @p procedure read_tfm_word;
  2072. begin tfm_byte(tfm_b0); tfm_byte(tfm_b1);
  2073. tfm_byte(tfm_b2); tfm_byte(tfm_b3);
  2074. if eof(tfm_file) then bad_tfm;
  2075. @ Here are three procedures used to check the consistency of font files:
  2076. First, the |check_check_sum| procedure compares two check sum values: a
  2077. warning is given if they differ and are both non-zero; if the second
  2078. value is not zero it replaces the first one.
  2079. Next, the |check_design_size| procedure compares two design size
  2080. values: a warning is given if they differ by more than a small amount.
  2081. Finally, the |check_width| procedure compares two character width
  2082. values: a warning is given if they differ.
  2083. @p procedure check_check_sum(@!f:font_number;@!c:int_32);
  2084.   {compare |font_check(f)| with |c|}
  2085. begin if (c<>font_check(f))and(c<>0) then
  2086.   begin
  2087.   if font_check(f)<>0 then
  2088.     begin print_ln('---beware: check sums do not agree!');
  2089. @.beware: check sums do not agree@>
  2090. @.check sums do not agree@>
  2091.     print_ln('   (',c:1,' vs. ',font_check(f):1,')');
  2092.     d_print('   ');
  2093.     mark_error;
  2094.     end;
  2095.   font_check(f):=c;
  2096.   end;
  2097. procedure check_design_size(@!f:font_number;@!d:int_32);
  2098.   {compare |font_design(f)| with |d|}
  2099. begin if abs(d-font_design(f))>2 then
  2100.   begin print_ln('---beware: design sizes do not agree!');
  2101. @.beware: design sizes do not agree@>
  2102. @.design sizes do not agree@>
  2103.   print_ln('   (',d:1,' vs. ',font_design(f):1,')');
  2104.   d_print('   ');
  2105.   mark_error;
  2106.   end;
  2107. procedure check_width(@!p:width_pointer;w:int_32);
  2108.   {compare |widths[p]| with |w|}
  2109. begin if w<>widths[p] then
  2110.   begin print_ln('---beware: char widths do not agree!');
  2111. @.beware: char widths do not agree@>
  2112. @.char widths do not agree@>
  2113.   print_ln('   (',w:1,' vs. ',widths[p]:1,')');
  2114.   d_print('   ');
  2115.   mark_error;
  2116.   end;
  2117. @ When processing a font definition we put the data extracted from the
  2118. \.{DVI} or \.{VF} file into the fields of |font_data[nf]| and call
  2119. |make_font| to obtain the internal font number for this font.
  2120. The function |make_font| determines if this font is already defined and,
  2121. if this is not the case, reads the \.{TFM} file.
  2122. @p function make_font:font_number;
  2123. var f:font_number; {internal font number of this font}
  2124. @!k:int_16; {loop index}
  2125. @!p:char_pointer; {index into |char_widths|}
  2126. @!q:width_pointer; {index into |widths|}
  2127. @!bc,@!ec:int_15; {first and last character in this font}
  2128. @!lh:int_15; {length of header in four byte words}
  2129. @!nw:int_15; {number of words in width table}
  2130. @!w:int_32; {a four byte integer}
  2131. @<Variables for scaling computation@>@;
  2132. begin f:=0;
  2133. while (font_name(f)<>font_name(nf))or@|
  2134.   (font_scaled(f)<>font_scaled(nf)) do incr(f);
  2135. d_print(' => ',f:1); print_font(f);
  2136. if f<nf then  begin check_check_sum(f,font_check(nf));
  2137.   check_design_size(f,font_design(nf));
  2138.   d_print(' loaded previously');
  2139.   end
  2140. else @<Define a new font@>;
  2141. print_ln('.');
  2142. make_font:=f;
  2143. @ If no font directory has been specified, \.{\title} is supposed to use
  2144. the default \.{TFM} directory, which is a system-dependent place where
  2145. the \.{TFM} files for standard fonts are kept.
  2146. The string variable |TFM_default_area| contains the name of this area.
  2147. @^system dependencies@>
  2148. @d TFM_default_area_name=='TeXfonts:' {change this to the correct name}
  2149. @d TFM_default_area_name_length=9 {change this to the correct length}
  2150. @<Glob...@>=
  2151. @!TFM_default_area:packed array[1..TFM_default_area_name_length] of char;
  2152. @ @<Set init...@>=
  2153. TFM_default_area:=TFM_default_area_name;
  2154. @ @<Define a new font@>=
  2155. begin if nf=max_fonts then overflow(str_fonts,max_fonts);
  2156.   font_type(f):=new_font_type;
  2157.   for k:=1 to TFM_default_area_name_length do
  2158.     cur_name[k]:=TFM_default_area[k];
  2159.   make_name(font_name(f),tfm_ext,TFM_default_area_name_length);
  2160.   @<TFM: Open |tfm_file|@>;
  2161.   @<TFM: Read past the header data@>;
  2162.   @<TFM: Store character-width indices@>;
  2163.   @<TFM: Read and convert the width values@>;
  2164.   @<TFM: Convert character-width indices to character-width pointers@>;
  2165.   @<TFM: Close |tfm_file|@>;
  2166.   d_print(' loaded at ',font_scaled(f):1,' DVI units');
  2167.   incr(nf);
  2168.   end
  2169. @ @<Glob...@>=
  2170. @!tfm_conv:real; {\.{DVI} units per absolute \.{TFM} unit}
  2171. @ We will use the following \.{WEB} macros to construct integers from
  2172. two or four of the four bytes read by |read_tfm_word|.
  2173. @^system dependencies@>
  2174. @d tfm_b01(#)== {|tfm_b0..tfm_b1| as non-negative integer}
  2175. if tfm_b0>127 then bad_tfm
  2176. else #:=tfm_b0*256+tfm_b1
  2177. @d tfm_b23(#)== {|tfm_b2..tfm_b3| as non-negative integer}
  2178. if tfm_b2>127 then bad_tfm
  2179. else #:=tfm_b2*256+tfm_b3
  2180. @d tfm_squad(#)== {|tfm_b0..tfm_b3| as signed integer}
  2181. if tfm_b0<128 then #:=((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3
  2182. else #:=(((tfm_b0-256)*256+tfm_b1)*256+tfm_b2)*256+tfm_b3
  2183. @d tfm_uquad== {|tfm_b0..tfm_b3| as unsigned integer}
  2184. (((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3)
  2185. @<TFM: Read past the header data@>=
  2186. read_tfm_word; tfm_b23(lh);
  2187. read_tfm_word; tfm_b01(bc); tfm_b23(ec);
  2188. if ec<bc then
  2189.   begin bc:=1; ec:=0;
  2190.   end
  2191. else if ec>255 then bad_tfm;
  2192. read_tfm_word; tfm_b01(nw);
  2193. if (nw=0)or(nw>256) then bad_tfm;
  2194. for k:=-2 to lh do
  2195.   begin read_tfm_word;
  2196.   if k=1 then  begin tfm_squad(w); check_check_sum(f,w);
  2197.     end
  2198.   else if k=2 then  begin if tfm_b0>127 then bad_tfm;
  2199.     check_design_size(f,round(tfm_conv*tfm_uquad));
  2200.     end;
  2201.   end
  2202. @ The width indices for the characters are stored in positions |n_chars|
  2203. through |n_chars-bc+ec+1| of the |char_widths| array; if characters on
  2204. either end of the range |bc..ec| do not exist, they are ignored and the
  2205. range is adjusted accordingly.
  2206. @<TFM: Store character-width indices@>=
  2207. read_tfm_word;
  2208. while (tfm_b0=0)and(bc<=ec) do
  2209.   begin incr(bc); read_tfm_word;
  2210.   end;
  2211. font_bc(f):=bc; font_chars(f):=n_chars-bc;
  2212. if ec>=max_chars-font_chars(f) then overflow(str_chars,max_chars);
  2213. for k:=bc to ec do
  2214.   begin char_widths[n_chars]:=tfm_b0; incr(n_chars); read_tfm_word;
  2215.   end;
  2216. while (char_widths[n_chars-1]=0)and(ec>=bc) do
  2217.   begin decr(n_chars); decr(ec);
  2218.   end;
  2219. font_ec(f):=ec
  2220. @ The most important part of |make_font| is the width computation, which
  2221. involves multiplying the relative widths in the \.{TFM} file by the
  2222. scaling factor in the \.{DVI} file. This fixed-point multiplication
  2223. must be done with precisely the same accuracy by all \.{DVI}-reading programs,
  2224. in order to validate the assumptions made by \.{DVI}-writing programs
  2225. like \TeX82.
  2226. Let us therefore summarize what needs to be done. Each width in a \.{TFM}
  2227. file appears as a four-byte quantity called a |fix_word|.  A |fix_word|
  2228. whose respective bytes are $(a,b,c,d)$ represents the number
  2229. $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
  2230. b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
  2231. -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
  2232. (No other choices of $a$ are allowed, since the magnitude of a \.{TFM}
  2233. dimension must be less than 16.)  We want to multiply this quantity by the
  2234. integer~|z|, which is known to be less than $2^{27}$.
  2235. If $|z|<2^{23}$, the individual multiplications $b\cdot z$, $c\cdot z$,
  2236. $d\cdot z$ cannot overflow; otherwise we will divide |z| by 2, 4, 8, or
  2237. 16, to obtain a multiplier less than $2^{23}$, and we can compensate for
  2238. this later. If |z| has thereby been replaced by $|z|^\prime=|z|/2^e$, let
  2239. $\beta=2^{4-e}$; we shall compute
  2240. $$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$ if $a=0$,
  2241. or the same quantity minus $\alpha=2^{4+e}z^\prime$ if $a=255$.
  2242. This calculation must be
  2243. done exactly, for the reasons stated above; the following program does the
  2244. job in a system-independent way, assuming that arithmetic is exact on
  2245. numbers less than $2^{31}$ in magnitude.
  2246. @ Since the dimensions extracted from a \.{VF} file have to be scaled in
  2247. exactly the same way as the \.{TFM} width values, we shall use the same
  2248. code in both cases; thus these computations need to be optimized for a
  2249. particular system only once.
  2250. @^system dependencies@>
  2251. @^optimization@>
  2252. @<Variables for scaling computation@>=
  2253. @!z:int_32; {multiplier}
  2254. @!alpha:int_32; {correction for negative values}
  2255. @!beta:int_15; {divisor}
  2256. @ @<Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$@>=
  2257. alpha:=16;
  2258. while z>=@'40000000 do
  2259.   begin z:=z div 2; alpha:=alpha+alpha;
  2260.   end;
  2261. beta:=256 div alpha; alpha:=alpha*z
  2262. @ @<Scaled value of |tfm_b1..tfm_b3|@>=
  2263. (((((tfm_b3*z)div@'400)+(tfm_b2*z))div@'400)+(tfm_b1*z))div beta
  2264. @ The first width value, which indicates that a character does not exist
  2265. and which must vanish, is converted to the width pointer value zero;
  2266. the other width values are scaled by |font_scaled(f)| and converted
  2267. to width pointers by |make_width|. The resulting width pointers are
  2268. stored temporarily in the |char_widths| array, following the with indices.
  2269. @<TFM: Read and convert the width values@>=
  2270. if nw-1>max_chars-n_chars then overflow(str_chars,max_chars);
  2271. if (tfm_b0<>0)or(tfm_b1<>0)or(tfm_b2<>0)or(tfm_b3<>0) then bad_tfm
  2272.   else char_widths[n_chars]:=0;
  2273. z:=font_scaled(f);
  2274. @!device font_space(f):=z div 6; {this is a 3-unit ``thin space''}
  2275. ecived @;
  2276. @<Replace |z|...@>;
  2277. for k:=1 to nw-1 do  begin read_tfm_word;
  2278.   w:=@<Scaled value of |tfm_b1..tfm_b3|@>;
  2279.   if tfm_b0>0 then if tfm_b0<255 then bad_tfm
  2280.     else Decr(w)(alpha);
  2281.   char_widths[n_chars+k]:=make_width(w);
  2282.   end
  2283. @ We simply translate the width indices into width pointers.
  2284. @<TFM: Convert character-width indices to character-width pointers@>=
  2285. for p:=font_chars(f)+bc to n_chars-1 do
  2286.   begin q:=char_widths[n_chars+char_widths[p]]; char_widths[p]:=q;
  2287.   @!device char_pixels[p]:=pix_widths[q]; @+ ecived @; @/
  2288.   end
  2289. @ The global variable |cur_fnt| contains the internal font number of
  2290. the currently selected font or the value |invalid_font| if no font has
  2291. been selected.
  2292. @<Glob...@>=
  2293. @!cur_fnt:font_number; {the currently selected font}
  2294. @* Low-level DVI input routines.
  2295. The program uses the binary file variable |dvi_file| for its main input
  2296. file; |dvi_loc| is the number of the byte about to be read next from
  2297. |dvi_file|.
  2298. @<Glob...@>=
  2299. @!dvi_file:byte_file; {the stuff we are \.{\title}ing}
  2300. @!dvi_loc:int_32; {where we are about to look, in |dvi_file|}
  2301. @ If the \.{DVI} file is badly malformed, we say |bad_dvi|; this
  2302. procedure gives an error message which refers the user to \.{DVItype},
  2303. and terminates \.{\title}.
  2304. @<Error handling...@>=
  2305. procedure bad_dvi;
  2306. begin print_ln(' ');
  2307. print_ln('Bad DVI file: loc=',dvi_loc:1,'!');
  2308. @.Bad DVI file@>
  2309. print(' Use DVItype with output level');
  2310. @.Use DVItype@>
  2311. if random_reading then print('=4') @+ else print('<4');
  2312. abort('to diagnose the problem');
  2313. @ To prepare |dvi_file| for input, we |reset| it.
  2314. @<Open input file(s)@>=
  2315. reset(dvi_file); {prepares to read packed bytes from |dvi_file|}
  2316. dvi_loc:=0;
  2317. @ For some operating systems it may be necessary to close |dvi_file|.
  2318. @<Close input file(s)@>=
  2319. @ Reading the \.{DVI} file should be done as efficient as possible for a
  2320. particular system; on many systems this means that a large number of
  2321. bytes from |dvi_file| is read into a buffer and will then be extracted
  2322. from that buffer. In order to simplify such system dependent changes
  2323. we use a pair of \.{WEB} macros: |dvi_byte| extracts the next \.{DVI}
  2324. byte and |dvi_eof| is |true| if we have reached the end of the \.{DVI}
  2325. file. Here we give simple minded definitions for these macros in terms
  2326. of standard \PASCAL.
  2327. @^system dependencies@>
  2328. @^optimization@>
  2329. @d dvi_eof == eof(dvi_file) {has the \.{DVI} file been exhausted?}
  2330. @d dvi_byte(#) ==
  2331.   if dvi_eof then bad_dvi
  2332.   else read(dvi_file,#) {obtain next \.{DVI} byte}
  2333. @ Next we come to the routines that are used only if |random_reading|    is
  2334. |true|. The driver program below needs two such routines: |dvi_length| should
  2335. compute the total number of bytes in |dvi_file|, possibly also
  2336. causing |eof(dvi_file)| to be true; and |dvi_move(n)| should position
  2337. |dvi_file| so that the next |dvi_byte| will read byte |n|, starting with
  2338. |n=0| for the first byte in the file.
  2339. @^system dependencies@>
  2340. Such routines are, of course, highly system dependent. They are implemented
  2341. here in terms of two assumed system routines called |set_pos| and |cur_pos|.
  2342. The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is
  2343. negative or larger than the total number of items in |f|; in the latter
  2344. case, |set_pos(f,n)| moves to the end of file |f|.
  2345. The call |cur_pos(f)| gives the total number of items in |f|, if
  2346. |eof(f)| is true; we use |cur_pos| only in such a situation.
  2347. @p function dvi_length:int_32;
  2348. begin set_pos(dvi_file,-1); dvi_length:=cur_pos(dvi_file);
  2349. procedure dvi_move(n:int_32);
  2350. begin set_pos(dvi_file,n); dvi_loc:=n;
  2351. @ We need seven simple functions to read the next byte or bytes
  2352. from |dvi_file|.
  2353. @p function dvi_sbyte:int_8; {returns the next byte, signed}
  2354. @!begin_byte(dvi_byte); incr(dvi_loc); comp_sbyte(dvi_sbyte);
  2355. function dvi_ubyte:int_8u; {returns the next byte, unsigned}
  2356. @!begin_byte(dvi_byte); incr(dvi_loc); comp_ubyte(dvi_ubyte);
  2357. function dvi_spair:int_16; {returns the next two bytes, signed}
  2358. @!begin_pair(dvi_byte); Incr(dvi_loc)(2); comp_spair(dvi_spair);
  2359. function dvi_upair:int_16u; {returns the next two bytes, unsigned}
  2360. @!begin_pair(dvi_byte); Incr(dvi_loc)(2); comp_upair(dvi_upair);
  2361. function dvi_strio:int_24; {returns the next three bytes, signed}
  2362. @!begin_trio(dvi_byte); Incr(dvi_loc)(3); comp_strio(dvi_strio);
  2363. function dvi_utrio:int_24u; {returns the next three bytes, unsigned}
  2364. @!begin_trio(dvi_byte); Incr(dvi_loc)(3); comp_utrio(dvi_utrio);
  2365. function dvi_squad:int_32; {returns the next four bytes, signed}
  2366. @!begin_quad(dvi_byte); Incr(dvi_loc)(4); comp_squad(dvi_squad);
  2367. @ Three other functions are used in cases where a four byte integer
  2368. (which is always signed) must have a non-negative value, a positive
  2369. value, or is a pointer which must be either positive or |=-1|.
  2370. @p function dvi_uquad:int_31; {result must be non-negative}
  2371. var x:int_32;
  2372. begin x:=dvi_squad; if x<0 then bad_dvi
  2373. else dvi_uquad:=x;
  2374. function dvi_pquad:int_31; {result must be positive}
  2375. var x:int_32;
  2376. begin x:=dvi_squad; if x<=0 then bad_dvi
  2377. else dvi_pquad:=x;
  2378. function dvi_pointer:int_32; {result must be positive or |=-1|}
  2379. var x:int_32;
  2380. begin x:=dvi_squad; if (x<=0)and(x<>-1) then bad_dvi
  2381. else dvi_pointer:=x;
  2382. @ Given the structure of the \.{DVI} commands it is fairly obvious
  2383. that their interpretation consists of two steps: First zero to four
  2384. bytes are read in order to obtain the value of the first parameter
  2385. (e.g., zero bytes for |set_char_0|, four bytes for |set4|); then,
  2386. depending on the command class, a specific action is performed (e.g.,
  2387. typeset a character but don't move the reference point for |put1..put4|).
  2388. The \.{DVItype} program uses large case statements for both steps;
  2389. unfortunately some \PASCAL\ compilers fail to implement large case
  2390. statements efficiently -- in particular those as the one used in the
  2391. |first_par| function of \.{DVItype}. Here we use a pair of look up tables:
  2392. |dvi_par| determines how to obtain the value of the first parameter, and
  2393. |dvi_cl| determines the command class.
  2394. A slight complication arises from the fact that we want to decompose the
  2395. character code of each character to be typset into a residue
  2396. |0<=char_res<256| and extension: |char_code=char_res+256*char_ext|;
  2397. the \.{TFM} widths as well as the pixel widths for a given resolution
  2398. are the same for all characters in a font with the same residue.
  2399. @d two_cases(#)==#,#+1
  2400. @d three_cases(#)==#,#+1,#+2
  2401. @d five_cases(#)==#,#+1,#+2,#+3,#+4
  2402. @ First we define the values used as array elements of |dvi_par|; we
  2403. distinguish between pure numbers and dimensions because dimensions read
  2404. from a \.{VF} file must be scaled.
  2405. @d char_par=0 {character for \\{set} and |put|}
  2406. @d no_par=1 {no parameter}
  2407. @d dim1_par=2 {one-byte signed dimension}
  2408. @d num1_par=3 {one-byte unsigned number}
  2409. @d dim2_par=4 {two-byte signed dimension}
  2410. @d num2_par=5 {two-byte unsigned number}
  2411. @d dim3_par=6 {three-byte signed dimension}
  2412. @d num3_par=7 {three-byte unsigned number}
  2413. @d dim4_par=8 {four-byte signed dimension}
  2414. @d num4_par=9 {four-byte signed number}
  2415. @d numu_par=10 {four-byte non-negative number}
  2416. @d rule_par=11 {dimensions for |set_rule| and |put_rule|}
  2417. @d fnt_par=12 {font for |fnt_num| commands}
  2418. @d max_par=12 {largest possible value}
  2419. @<Types...@>=
  2420. @!cmd_par=char_par..max_par;
  2421. @ Here we declare the array |dvi_par|.
  2422. @<Globals...@>=
  2423. @!dvi_par:packed array [eight_bits] of cmd_par;
  2424. @ And here we initialize it.
  2425. @<Set init...@>=
  2426. for i:=0 to put1+3 do dvi_par[i]:=char_par;@/
  2427. for i:=nop to 255 do dvi_par[i]:=no_par;@/
  2428. dvi_par[set_rule]:=rule_par; dvi_par[put_rule]:=rule_par;@/
  2429. dvi_par[right1]:=dim1_par; dvi_par[right1+1]:=dim2_par;
  2430. dvi_par[right1+2]:=dim3_par; dvi_par[right1+3]:=dim4_par;@/
  2431. for i:=fnt_num_0 to fnt_num_0+63 do dvi_par[i]:=fnt_par;@/
  2432. dvi_par[fnt1]:=num1_par; dvi_par[fnt1+1]:=num2_par;
  2433. dvi_par[fnt1+2]:=num3_par; dvi_par[fnt1+3]:=num4_par;@/
  2434. dvi_par[xxx1]:=num1_par; dvi_par[xxx1+1]:=num2_par;
  2435. dvi_par[xxx1+2]:=num3_par; dvi_par[xxx1+3]:=numu_par;@/
  2436. for i:=0 to 3 do
  2437.   begin dvi_par[i+w1]:=dvi_par[i+right1];
  2438.   dvi_par[i+x1]:=dvi_par[i+right1];
  2439.   dvi_par[i+down1]:=dvi_par[i+right1];
  2440.   dvi_par[i+y1]:=dvi_par[i+right1];
  2441.   dvi_par[i+z1]:=dvi_par[i+right1];
  2442.   dvi_par[i+fnt_def1]:=dvi_par[i+fnt1];
  2443.   end;
  2444. @ Next we define the values used as array elements of |dvi_cl|;
  2445. several \.{DVI} commands (e.g., |nop|, |bop|, |eop|, |pre|, |post|) will
  2446. allways be treated separately and are therfore assigned to the invalid
  2447. class here.
  2448. @d char_cl=0
  2449. @d rule_cl=char_cl+1
  2450. @d xxx_cl=char_cl+2
  2451. @d push_cl=3
  2452. @d pop_cl=4
  2453. @d w0_cl=5
  2454. @d x0_cl=w0_cl+1
  2455. @d right_cl=w0_cl+2
  2456. @d w_cl=w0_cl+3
  2457. @d x_cl=w0_cl+4
  2458. @d y0_cl=10
  2459. @d z0_cl=y0_cl+1
  2460. @d down_cl=y0_cl+2
  2461. @d y_cl=y0_cl+3
  2462. @d z_cl=y0_cl+4
  2463. @d fnt_cl=15
  2464. @d fnt_def_cl=16
  2465. @d invalid_cl=17
  2466. @d max_cl=invalid_cl {largest possible value}
  2467. @<Types...@>=
  2468. @!cmd_cl=char_cl..max_cl;
  2469. @ Here we declare the array |dvi_cl|.
  2470. @<Globals...@>=
  2471. @!dvi_cl:packed array [eight_bits] of cmd_cl;
  2472. @ And here we initialize it.
  2473. @<Set init...@>=
  2474. for i:=set_char_0 to put1+3 do dvi_cl[i]:=char_cl;
  2475. dvi_cl[set_rule]:=rule_cl; dvi_cl[put_rule]:=rule_cl;@/
  2476. dvi_cl[nop]:=invalid_cl;
  2477. dvi_cl[bop]:=invalid_cl; dvi_cl[eop]:=invalid_cl;@/
  2478. dvi_cl[push]:=push_cl; dvi_cl[pop]:=pop_cl;@/
  2479. dvi_cl[w0]:=w0_cl; dvi_cl[x0]:=x0_cl;@/
  2480. dvi_cl[y0]:=y0_cl; dvi_cl[z0]:=z0_cl;@/
  2481. for i:=0 to 3 do
  2482.   begin dvi_cl[i+right1]:=right_cl;
  2483.   dvi_cl[i+w1]:=w_cl;
  2484.   dvi_cl[i+x1]:=x_cl;@/
  2485.   dvi_cl[i+down1]:=down_cl;
  2486.   dvi_cl[i+y1]:=y_cl;
  2487.   dvi_cl[i+z1]:=z_cl;@/
  2488.   dvi_cl[i+xxx1]:=xxx_cl;
  2489.   dvi_cl[i+fnt_def1]:=fnt_def_cl;
  2490.   end;
  2491. for i:=fnt_num_0 to fnt1+3 do dvi_cl[i]:=fnt_cl;
  2492. for i:=pre to 255 do dvi_cl[i]:=invalid_cl;
  2493. @ A few small arrays are used to generate \.{DVI} commands.
  2494. @<Glob...@>=
  2495. @!dvi_char_cmd:array[boolean] of eight_bits; {|put1| and |set1|}
  2496. @!dvi_rule_cmd:array[boolean] of eight_bits; {|put_rule| and |set_rule|}
  2497. @!dvi_right_cmd:array[right_cl..x_cl] of eight_bits; {|right1|, |w1|, and |x1|}
  2498. @!dvi_down_cmd:array[down_cl..z_cl] of eight_bits; {|down1|, |y1|, and |z1|}
  2499. @ @<Set init...@>=
  2500. dvi_char_cmd[false]:=put1;
  2501. dvi_char_cmd[true]:=set1;@/
  2502. dvi_rule_cmd[false]:=put_rule;
  2503. dvi_rule_cmd[true]:=set_rule;@/
  2504. dvi_right_cmd[right_cl]:=right1;
  2505. dvi_right_cmd[w_cl]:=w1;
  2506. dvi_right_cmd[x_cl]:=x1;@/
  2507. dvi_down_cmd[down_cl]:=down1;
  2508. dvi_down_cmd[y_cl]:=y1;
  2509. dvi_down_cmd[z_cl]:=z1;
  2510. @ The global variables |cur_cmd|, |cur_parm| and |cur_class| are used
  2511. for the current \.{DVI} command, its first parameter (if any), and its
  2512. command class respectively.
  2513. @<Glob...@>=
  2514. @!cur_cmd:eight_bits; {current \.{DVI} command byte}
  2515. @!cur_parm:int_32; {its first parameter (if any)}
  2516. @!cur_class:cmd_cl; {its class}
  2517. @ When typesetting a character, |cur_ext| and |cur_res| are its
  2518. extension and residue; when typesetting a character or rule, the boolean
  2519. variable |cur_upd| is |true| for \\{set} commands, |false| for |put|
  2520. commands.
  2521. @<Glob...@>=
  2522. @!cur_ext:int_24; {the current character extension}
  2523. @!cur_res:int_8u; {the current character residue}
  2524. @!cur_wp:width_pointer; {width pointer of the current character}
  2525. @!cur_upd:boolean; {is this a \\{set} or |set_rule| command ?}
  2526. @!cur_v_dimen:int_32; {a vertical dimension}
  2527. @!cur_h_dimen:int_32; {a horizontal dimension}
  2528. @ The |dvi_first_par| procedure first reads \.{DVI} command bytes into
  2529. |cur_cmd| until |cur_cmd<>nop|; then |cur_parm| is set to the value of
  2530. the first parameter (if any) and |cur_class| to the command class.
  2531. @p procedure dvi_first_par;
  2532. begin repeat cur_cmd:=dvi_ubyte;
  2533. until cur_cmd<>nop; {skip over |nop|s}
  2534. case dvi_par[cur_cmd] of
  2535. char_par: if cur_cmd<set1 then
  2536.   begin cur_ext:=0; cur_res:=cur_cmd; cur_upd:=true
  2537.   end
  2538. else  begin cur_upd:=(cur_cmd<put1);
  2539.   case cur_cmd-dvi_char_cmd[cur_upd] of
  2540.   0: cur_ext:=0;
  2541.   1: cur_ext:=dvi_ubyte;
  2542.   2: cur_ext:=dvi_upair;
  2543.   3: cur_ext:=dvi_strio;
  2544.   end;
  2545.   cur_res:=dvi_ubyte;
  2546.   end;
  2547. no_par: do_nothing;
  2548. dim1_par: cur_parm:=dvi_sbyte;
  2549. num1_par: cur_parm:=dvi_ubyte;
  2550. dim2_par: cur_parm:=dvi_spair;
  2551. num2_par: cur_parm:=dvi_upair;
  2552. dim3_par: cur_parm:=dvi_strio;
  2553. num3_par: cur_parm:=dvi_utrio;
  2554. two_cases(dim4_par): cur_parm:=dvi_squad; {|dim4_par| and |num4_par|}
  2555. numu_par: cur_parm:=dvi_uquad;
  2556. rule_par:
  2557.   begin cur_v_dimen:=dvi_squad; cur_h_dimen:=dvi_squad;
  2558.   cur_upd:=(cur_cmd=set_rule);
  2559.   end;
  2560. fnt_par:cur_parm:=cur_cmd-fnt_num_0;
  2561. cur_class:=dvi_cl[cur_cmd];
  2562. @ The global variable |dvi_nf| is used for the number of different
  2563. \.{DVI} fonts defined so far; their external font numbers (as extracted
  2564. from the \.{DVI} file) are stored in the array |dvi_e_fnts|, the
  2565. corresponding internal font numbers used internally by \.{\title} are
  2566. stored in the array |dvi_i_fnts|.
  2567. @<Glob...@>=
  2568. @!dvi_e_fnts:array[font_number] of int_32; {external font numbers}
  2569. @!dvi_i_fnts:array[font_number] of font_number; {corresponding
  2570.   internal font numbers}
  2571. @!dvi_nf:font_number; {number of \.{DVI} fonts defined so far}
  2572. @ @<Set ini...@>=
  2573. dvi_nf:=0;
  2574. @ The |dvi_font| procedure sets |cur_fnt| to the internal font number
  2575. corresponding to the external font number |cur_parm| (or aborts the
  2576. program if such a font was never defined).
  2577. @p procedure dvi_font; {computes |cur_fnt| corresponding to |cur_parm|}
  2578. var f:font_number; {where the font is sought}
  2579. begin @<DVI: Locate font |cur_parm|@>;
  2580. if f=dvi_nf then bad_dvi;
  2581. cur_fnt:=dvi_i_fnts[f];
  2582. @ @<DVI: Locate font |cur_parm|@>=
  2583. f:=0; dvi_e_fnts[dvi_nf]:=cur_parm;
  2584. while cur_parm<>dvi_e_fnts[f] do incr(f)
  2585. @ Finally the |dvi_do_font| procedure is called when one of the command
  2586. |fnt_def1..fnt_def4| and its first parameter have been read from the
  2587. \.{DVI} file; the argument indicates whether this should be the second
  2588. definition of the font (|true|) or not (|false|).
  2589. @p procedure dvi_do_font(@!second:boolean);
  2590. var f:font_number; {where the font is sought}
  2591. @!k:int_15; {general purpose variable}
  2592. begin print('DVI: font ',cur_parm:1);
  2593. @<DVI: Locate font |cur_parm|@>;
  2594. if (f=dvi_nf)=second then bad_dvi;
  2595. font_check(nf):=dvi_squad;
  2596. font_scaled(nf):=dvi_pquad;
  2597. font_design(nf):=dvi_pquad;
  2598. k:=dvi_ubyte; pckt_room(1); append_byte(k);
  2599. Incr(k)(dvi_ubyte); pckt_room(k);
  2600. while k>0 do  begin append_byte(dvi_ubyte); decr(k);
  2601.   end;
  2602. font_name(nf):=make_packet; {the font area plus name}
  2603. dvi_i_fnts[dvi_nf]:=make_font;
  2604. if not second then
  2605.   begin if dvi_nf=max_fonts then overflow(str_fonts,max_fonts);
  2606.   incr(dvi_nf);
  2607.   end
  2608. else if dvi_i_fnts[f]<>dvi_i_fnts[dvi_nf] then bad_dvi;
  2609. @* Low-level VF input routines.
  2610. The program uses the binary file variable |vf_file| for input from \.{VF}
  2611. files; |vf_loc| is the number of the byte about to be read next from
  2612. |vf_file|.
  2613. @<Glob...@>=
  2614. @!vf_file:byte_file; {a \.{VF} file}
  2615. @!vf_loc:int_32; {where we are about to look, in |vf_file|}
  2616. @!vf_limit:int_32; {value of |vf_loc| at end of a character packet}
  2617. @!vf_ext:pckt_pointer; {extension for \.{VF} files}
  2618. @!cur_vf:font_number; {font number of current \.{VF} file}
  2619. @ @<Initialize predefined strings@>=
  2620. id3(".")("V")("F")(vf_ext); {file name extension for \.{VF} files}
  2621. @ If a \.{VF} file is badly malformed, we say |bad_vf|; this procedure
  2622. gives an error message which refers the user to \.{VFtoVP} and \.{VPtoVF},
  2623. and terminates \.{\title}.
  2624. @<Error handling...@>=
  2625. procedure bad_vf;
  2626. begin print_ln(' ');
  2627. print('Bad VF file'); print_font(cur_vf); print_ln(': loc=',vf_loc:1,'!');
  2628. @.Bad VF file@>
  2629. abort('Use VFtoVP/VPtoVF to diagnose and correct the problem');
  2630. @.Use VFtoVP/VPtoVF@>
  2631. @ To prepare |vf_file| for input we |reset| it.
  2632. @<VF: Open |vf_file| or |goto not_found|@>=
  2633. reset(vf_file,cur_name);
  2634. @^system dependencies@>
  2635. if eof(vf_file) then
  2636.   goto not_found;
  2637. vf_loc:=0
  2638. @ For some operating systems it may be necessary to close |vf_file|.
  2639. @<VF: Close |vf_file|@>=
  2640. @ Reading a \.{VF} file should be done as efficient as possible for a
  2641. particular system; on many systems this means that a large number of
  2642. bytes from |vf_file| is read into a buffer and will then be extracted
  2643. from that buffer. In order to simplify such system dependent changes
  2644. we use a pair of \.{WEB} macros: |vf_byte| extracts the next \.{VF}
  2645. byte and |vf_eof| is |true| if we have reached the end of the \.{VF}
  2646. file. Here we give simple minded definitions for these macros in terms
  2647. of standard \PASCAL.
  2648. @^system dependencies@>
  2649. @^optimization@>
  2650. @d vf_eof == eof(vf_file) {has the \.{VF} file been exhausted?}
  2651. @d vf_byte(#) ==
  2652.   if vf_eof then bad_vf
  2653.   else read(vf_file,#) {obtain next \.{VF} byte}
  2654. @ We need several simple functions to read the next byte or bytes
  2655. from |vf_file|.
  2656. @p function vf_ubyte:int_8u; {returns the next byte, unsigned}
  2657. @!begin_byte(vf_byte); incr(vf_loc); comp_ubyte(vf_ubyte);
  2658. function vf_upair:int_16u; {returns the next two bytes, unsigned}
  2659. @!begin_pair(vf_byte); Incr(vf_loc)(2); comp_upair(vf_upair);
  2660. function vf_strio:int_24; {returns the next three bytes, signed}
  2661. @!begin_trio(vf_byte); Incr(vf_loc)(3); comp_strio(vf_strio);
  2662. function vf_utrio:int_24u; {returns the next three bytes, unsigned}
  2663. @!begin_trio(vf_byte); Incr(vf_loc)(3); comp_utrio(vf_utrio);
  2664. function vf_squad:int_32; {returns the next four bytes, signed}
  2665. @!begin_quad(vf_byte); Incr(vf_loc)(4); comp_squad(vf_squad);
  2666. @ All dimensions in a \.{VF} file, except the design sizes of a virtual
  2667. font and its local fonts, are |fix_word|s that must be scaled in exactly
  2668. the same way as the character widths from a \.{TFM} file; we can use the
  2669. same code, but this time |z|, |alpha|, and |beta| are global variables.
  2670. @<Glob...@>=
  2671. @<Variables for scaling computation@>@;
  2672. @ We need five functions to read the next byte or bytes and convert a
  2673. |fix_word| to a scaled dimension.
  2674. @p function vf_fix1:int_32; {returns the next byte as scaled value}
  2675. var x:int_32; {accumulator}
  2676. begin vf_byte(tfm_b3); incr(vf_loc);
  2677. if tfm_b3>127 then tfm_b1:=255 @+ else tfm_b1:=0;
  2678. tfm_b2:=tfm_b1;
  2679. x:=@<Scaled value of |tfm_b1..tfm_b3|@>;
  2680. if tfm_b1>127 then Decr(x)(alpha);
  2681. vf_fix1:=x;
  2682. function vf_fix2:int_32; {returns the next two bytes as scaled value}
  2683. var x:int_32; {accumulator}
  2684. begin vf_byte(tfm_b2); vf_byte(tfm_b3); Incr(vf_loc)(2);
  2685. if tfm_b2>127 then tfm_b1:=255 @+ else tfm_b1:=0;
  2686. x:=@<Scaled value of |tfm_b1..tfm_b3|@>;
  2687. if tfm_b1>127 then Decr(x)(alpha);
  2688. vf_fix2:=x;
  2689. function vf_fix3:int_32; {returns the next three bytes as scaled value}
  2690. var x:int_32; {accumulator}
  2691. begin vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
  2692. Incr(vf_loc)(3);@/
  2693. x:=@<Scaled value of |tfm_b1..tfm_b3|@>;
  2694. if tfm_b1>127 then Decr(x)(alpha);
  2695. vf_fix3:=x;
  2696. function vf_fix3u:int_32; {returns the next three bytes as scaled value}
  2697. begin vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
  2698. Incr(vf_loc)(3);@/
  2699. vf_fix3u:=@<Scaled value of |tfm_b1..tfm_b3|@>;
  2700. function vf_fix4:int_32; {returns the next four bytes as scaled value}
  2701. var x:int_32; {accumulator}
  2702. begin vf_byte(tfm_b0); vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
  2703. Incr(vf_loc)(4);@/
  2704. x:=@<Scaled value of |tfm_b1..tfm_b3|@>;
  2705. if tfm_b0>0 then
  2706.   if tfm_b0<255 then bad_vf @+ else Decr(x)(alpha);
  2707. vf_fix4:=x;
  2708. @ Three other functions are used in cases where the result must have a
  2709. non-negative value or a positive value.
  2710. @p function vf_uquad:int_31; {result must be non-negative}
  2711. var x:int_32;
  2712. begin x:=vf_squad; if x<0 then bad_vf @+ else vf_uquad:=x;
  2713. function vf_pquad:int_31; {result must be positive}
  2714. var x:int_32;
  2715. begin x:=vf_squad; if x<=0 then bad_vf @+ else vf_pquad:=x;
  2716. function vf_fixp:int_31; {result must be positive}
  2717. var x:int_32; {accumulator}
  2718. begin vf_byte(tfm_b0); vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
  2719. Incr(vf_loc)(4);@/
  2720. x:=@<Scaled value of |tfm_b1..tfm_b3|@>;
  2721. if tfm_b0>0 then bad_vf;
  2722. vf_fixp:=x;
  2723. @ The |vf_first_par| procedure first reads a \.{VF} command byte into
  2724. |cur_cmd|; then |cur_parm| is set to the value of the first parameter
  2725. (if any) and |cur_class| to the command class.
  2726. @d set_cur_wp == {set |cur_wp| to the char's width pointer}
  2727. cur_wp:=invalid_width;
  2728. if cur_fnt<>invalid_font then
  2729.   if (cur_res>=font_bc(cur_fnt))and(cur_res<=font_ec(cur_fnt)) then
  2730.     cur_wp:=font_width(cur_fnt)(cur_res)
  2731. @p procedure vf_first_par;
  2732. begin cur_cmd:=vf_ubyte;
  2733. case dvi_par[cur_cmd] of
  2734. char_par:
  2735.   begin if cur_cmd<set1 then
  2736.     begin cur_ext:=0; cur_res:=cur_cmd; cur_upd:=true
  2737.     end
  2738.   else  begin cur_upd:=(cur_cmd<put1);
  2739.     case cur_cmd-dvi_char_cmd[cur_upd] of
  2740.     0: cur_ext:=0;
  2741.     1: cur_ext:=vf_ubyte;
  2742.     2: cur_ext:=vf_upair;
  2743.     3: cur_ext:=vf_strio;
  2744.     end;
  2745.     cur_res:=vf_ubyte;
  2746.     end;
  2747.   set_cur_wp; if cur_wp=invalid_width then bad_vf;
  2748.   end;
  2749. no_par: do_nothing;
  2750. dim1_par: cur_parm:=vf_fix1;
  2751. num1_par: cur_parm:=vf_ubyte;
  2752. dim2_par: cur_parm:=vf_fix2;
  2753. num2_par: cur_parm:=vf_upair;
  2754. dim3_par: cur_parm:=vf_fix3;
  2755. num3_par: cur_parm:=vf_utrio;
  2756. dim4_par: cur_parm:=vf_fix4;
  2757. num4_par: cur_parm:=vf_squad;
  2758. numu_par: cur_parm:=vf_uquad;
  2759. rule_par:
  2760.   begin cur_v_dimen:=vf_fix4; cur_h_dimen:=vf_fix4;
  2761.   cur_upd:=(cur_cmd=set_rule);
  2762.   end;
  2763. fnt_par:cur_parm:=cur_cmd-fnt_num_0;
  2764. cur_class:=dvi_cl[cur_cmd];
  2765. @ Here we define the additional |font_data| fields required for virtual
  2766. fonts; |font_vf_fnt(f)| is the default font for character packets from
  2767. virtual font~|f|, |font_vf_packet(f)(c)| is the character packet for
  2768. character~|c| from virtual font~|f|.
  2769. In order to simplify the \.{web2c} translation the fields in the variant
  2770. for |f_type=vf_font_type| are accessed through the \.{WEB} macro
  2771. |vf_font_data|.
  2772. @^font types@>@.web2c@>
  2773. @d vf_font_data(#)==font_data[#] {access |vf_font_type| variant fields}
  2774. @d font_vf_chars(#)==vf_font_data(#).vf_chars_field {character packet offset}
  2775. @d font_vf_fnt(#)==vf_font_data(#).vf_fnt_field {font number of default font}
  2776. @d font_vf_packet_end(#)==#]
  2777. @d font_vf_packet(#)==char_packets[font_vf_chars(#)+font_vf_packet_end
  2778. @<Cases for |font_record|@>=
  2779. vf_font_type:
  2780.  (@!vf_chars_field:char_offset; {character packet offset}
  2781.   @!vf_fnt_field:font_number); {font number of default font}
  2782. @ The global variable |vf_nf| is used for the number of different local
  2783. fonts defined in a \.{VF} file so far; their external font numbers (as
  2784. extracted from the \.{VF} file) are stored in the array |vf_e_fnts|, the
  2785. corresponding internal font numbers used internally by \.{\title} are
  2786. stored in the array |vf_i_fnts|.
  2787. @<Glob...@>=
  2788. @!vf_e_fnts:array[font_number] of int_32; {external font numbers}
  2789. @!vf_i_fnts:array[font_number] of font_number; {corresponding
  2790.   internal font numbers}
  2791. @!vf_nf:font_number; {number of local fonts defined so far}
  2792. @!lcl_nf:font_number; {largest |vf_nf| value for any \.{VF} file}
  2793. @ @<Set init...@>=
  2794. lcl_nf:=0;
  2795. @ The |vf_font| procedure sets |cur_fnt| to the internal font number
  2796. corresponding to the external font number |cur_parm| (or aborts the
  2797. program if such a font was never defined).
  2798. @p procedure vf_font; {computes |cur_fnt| corresponding to |cur_parm|}
  2799. var f:font_number; {where the font is sought}
  2800. begin @<VF: Locate font |cur_parm|@>;
  2801. if f=vf_nf then bad_vf;
  2802. cur_fnt:=vf_i_fnts[f];
  2803. @ @<VF: Locate font |cur_parm|@>=
  2804. f:=0; vf_e_fnts[vf_nf]:=cur_parm;
  2805. while cur_parm<>vf_e_fnts[f] do incr(f)
  2806. @ Finally the |vf_do_font| procedure is called when one of the command
  2807. |fnt_def1..fnt_def4| and its first parameter have been read from the
  2808. \.{VF} file.
  2809. @p procedure vf_do_font;
  2810. var f:font_number; {where the font is sought}
  2811. @!k:int_15; {general purpose variable}
  2812. begin print('VF: font ',cur_parm:1);@/
  2813. @<VF: Locate font |cur_parm|@>;
  2814. if f<>vf_nf then bad_vf;
  2815. font_check(nf):=vf_squad;
  2816. font_scaled(nf):=vf_fixp;
  2817. font_design(nf):=round(tfm_conv*vf_pquad);
  2818. k:=vf_ubyte; pckt_room(1); append_byte(k);
  2819. Incr(k)(vf_ubyte); pckt_room(k);
  2820. while k>0 do  begin append_byte(vf_ubyte); decr(k);
  2821.   end;
  2822. font_name(nf):=make_packet; {the font area plus name}
  2823. vf_i_fnts[vf_nf]:=make_font;
  2824. if vf_nf=lcl_nf then
  2825.   if lcl_nf=max_fonts then overflow(str_fonts,max_fonts)
  2826.   else incr(lcl_nf);
  2827. incr(vf_nf);
  2828. @* Reading VF files.
  2829. First we need a few global variables: |cur_vf_ext| and |cur_vf_res|
  2830. are the extension and residue of the character we are building a packet
  2831. for; |vf_fnt| is the current font of the packet being built (whereas
  2832. |cur_fnt| is the current font of the packet read from the \.{VF} file).
  2833. @<Glob...@>=
  2834. @!cur_vf_ext:int_24; {character extension for the current packet}
  2835. @!cur_vf_res:int_8u; {character residue for the current packet}
  2836. @!cur_vf_wp:width_pointer; {width pointer for the current packet}
  2837. @!vf_fnt:font_number; {current font in the current packet}
  2838. @ The \.{VF} format specifies that the interpretation of each packet
  2839. begins with |w=x=y=z=0|; any |w0|, |x0|, |y0|, or |z0| command using
  2840. these initial values will be ignored.
  2841. @<Types...@>=
  2842. @!vf_state=array[0..1,0..1] of boolean; {state of |w|, |x|, |y|, and |z|}
  2843. @ As implied by the \.{VF} format the \.{DVI} commands read from the
  2844. \.{VF} file are enclosed by |push| and |pop|; as we read \.{DVI}
  2845. commands and append them to |byte_mem|, we perform a set of
  2846. transformations in order to simplify the resulting packet: Let |zero| be
  2847. any of the commands |put|, |put_rule|, |fnt_num|, |fnt|, or |xxx| which
  2848. all leave the current position on the page unchanged, let |move| be any
  2849. of the horizontal or vertical movement commands |right1..z4|, and let
  2850. |any| be any sequence of commands containing |push| and |pop| in
  2851. properly nested pairs; whenever possible we apply one of the following
  2852. transformation rules: $$\def\n#1:{\hbox to 3cm{\hfil#1:}}
  2853. \leqalignno{
  2854. \hbox{|push| |zero|}&\RA\hbox{|zero| |push|}&\n1:\cr
  2855. \hbox{|move| |pop|}&\RA\hbox{|pop|}&\n2:\cr
  2856. \hbox{|push| |pop|}&\RA{}&\n3:\cr
  2857. \hbox{|push| |set_char| |pop|}&\RA\hbox{|put|}&\n4a:\cr
  2858. \hbox{|push| \\{set} |pop|}&\RA\hbox{|put|}&\n4b:\cr
  2859. \hbox{|push| |set_rule| |pop|}&\RA\hbox{|put_rule|}&\n4c:\cr
  2860. \hbox{|push| |push| |any| |pop|}&\RA\hbox{|push| |any| |pop| |push|}&\n5:\cr
  2861. \hbox{|push| |any| |pop| |pop|}&\RA\hbox{|any| |pop|}&\n6:\cr
  2862. @ In order to perform these transformations we need a stack which is
  2863. indexed by |vf_ptr|, the number of |push| commands without corresponding
  2864. |pop| in the packet we are building; the |vf_push_loc| array contains
  2865. the locations in |byte_mem| following such |push| commands.
  2866. In view of rule~5 consecutive |push| commands are never stored, the
  2867. |vf_push_num| array is used to count them.
  2868. The |vf_last| array indicates the type of the last non-discardable item:
  2869. a character, a rule, or a group enclosed by |push| and |pop|;
  2870. the |vf_last_end| array points to the ending locations and, if
  2871. |vf_last<>vf_other|, the |vf_last_loc| array points to the starting
  2872. locations of these items.
  2873. @d vf_set=0 {|vf_set=char_cl|, last item is a |set_char| or \\{set}}
  2874. @d vf_rule=1 {|vf_rule=rule_cl|, last item is a |set_rule|}
  2875. @d vf_group=2 {last item is a group enclosed by |push| and |pop|}
  2876. @d vf_put=3 {last item is a |put|}
  2877. @d vf_other=4 {last item (if any) is none of the above}
  2878. @<Types...@>=
  2879. @!vf_type=vf_set..vf_other;
  2880. @ @<Glob...@>=
  2881. @!vf_move: array[stack_pointer] of vf_state; {state of |w|, |x|, |y|, and |z|}
  2882. @!vf_push_loc: array[stack_pointer] of byte_pointer; {end of a |push|}
  2883. @!vf_last_loc: array[stack_pointer] of byte_pointer; {start of an item}
  2884. @!vf_last_end: array[stack_pointer] of byte_pointer; {end of an item}
  2885. @!vf_push_num: array[stack_pointer] of eight_bits; {|push| count}
  2886. @!vf_last: array[stack_pointer] of vf_type; {type of last item}
  2887. @!vf_ptr:stack_pointer; {current number of unfinished groups}
  2888. @!stack_used:stack_pointer; {largest |vf_ptr| or |stack_ptr| value}
  2889. @ We use two small arrays to determine the item type of a character or a
  2890. rule.
  2891. @<Glob...@>=
  2892. @!vf_char_type:array[boolean] of vf_type;
  2893. @!vf_rule_type:array[boolean] of vf_type;
  2894. @ @<Set init...@>=
  2895. vf_move[0][0][0]:=false; vf_move[0][0][1]:=false;
  2896. vf_move[0][1][0]:=false; vf_move[0][1][1]:=false;@/
  2897. stack_used:=0;@/
  2898. vf_char_type[false]:=vf_put; vf_char_type[true]:=vf_set;@/
  2899. vf_rule_type[false]:=vf_other; vf_rule_type[true]:=vf_rule;
  2900. @ The |vf_do_char| procedure is used to read, analyze, and store a
  2901. character packet.
  2902. @p procedure vf_do_char; {read and store a \.{VF} packet}
  2903. label reswitch,done;
  2904. var temp_byte:int_8u; {byte for temporary variables}
  2905. @!temp_int:int_32; {integer for temporary variables}
  2906. @!k:byte_pointer; {index into |byte_mem|}
  2907. @!move_zero:boolean; {|true| if rule 1 is used}
  2908. @!last_pop:boolean; {|true| if final |pop| has been manufactured}
  2909. begin @<VF: Initialize the character packet@>;@/
  2910. @<VF: Append \.{DVI} commands to the character packet@>;@/
  2911. @<VF: Build the final form of the character packet@>;@/
  2912. @ Here we read the first bytes of a character packet from the \.{VF}
  2913. file and initialize the packet being built in |byte_mem|; the start of
  2914. the whole packet is stored in |vf_push_loc[0]|.
  2915. @<VF: Initialize the character packet@>=
  2916. if cur_cmd<long_char then
  2917.   begin cur_parm:=cur_cmd;
  2918.   cur_vf_ext:=0; cur_vf_res:=vf_ubyte; temp_int:=vf_fix3u;
  2919.   end
  2920. else  begin cur_parm:=vf_uquad;
  2921.   cur_vf_ext:=vf_strio; cur_vf_res:=vf_ubyte; temp_int:=vf_fix4;
  2922.   end;
  2923. if (cur_vf_res>=font_bc(cur_vf))and(cur_vf_res<=font_ec(cur_vf)) then
  2924.   cur_vf_wp:=font_width(cur_vf)(cur_vf_res)
  2925. else cur_vf_wp:=invalid_width;
  2926. if cur_vf_wp=invalid_width then bad_vf;
  2927. check_width(cur_vf_wp,temp_int);
  2928. vf_limit:=vf_loc+cur_parm;
  2929. cur_fnt:=font_vf_fnt(cur_vf); vf_fnt:=cur_fnt;@/
  2930. start_packet(cur_vf_ext,font_vf_packet(cur_vf)(cur_vf_res),0);@/
  2931. vf_push_loc[0]:=byte_ptr; vf_last_end[0]:=byte_ptr;
  2932. vf_last[0]:=vf_other; vf_ptr:=0
  2933. @ For every \.{DVI} command read from the \.{VF} file some action is
  2934. performed; in addition the initial |push| and the final |pop| are
  2935. manufactured here.
  2936. @<VF: Append \.{DVI} commands to the character packet@>=
  2937. last_pop:=false;
  2938. cur_class:=push_cl; {initial |push|}
  2939. loop  begin
  2940. reswitch:case cur_class of
  2941.   three_cases(char_cl): @<VF: Do a |char|, |rule|, or |xxx|@>;
  2942.   push_cl: @<VF: Do a |push|@>;
  2943.   pop_cl: @<VF: Do a |pop|@>;
  2944.   two_cases(w0_cl):
  2945.     if vf_move[vf_ptr][0][cur_class-w0_cl] then append_one(cur_cmd);
  2946.   three_cases(right_cl):
  2947.     begin pckt_signed(dvi_right_cmd[cur_class],cur_parm);
  2948.     if cur_class>=w_cl then vf_move[vf_ptr][0][cur_class-w_cl]:=true;
  2949.     end;
  2950.   two_cases(y0_cl):
  2951.     if vf_move[vf_ptr][1][cur_class-y0_cl] then append_one(cur_cmd);
  2952.   three_cases(down_cl):
  2953.     begin pckt_signed(dvi_down_cmd[cur_class],cur_parm);
  2954.     if cur_class>=y_cl then vf_move[vf_ptr][1][cur_class-y_cl]:=true;
  2955.     end;
  2956.   fnt_cl: vf_font;
  2957.   fnt_def_cl: bad_vf;
  2958.   invalid_cl: if cur_cmd<>nop then bad_vf;
  2959.   end; {there are no other cases}
  2960.   if vf_loc<vf_limit then vf_first_par
  2961.   else if last_pop then goto done
  2962.   else  begin cur_class:=pop_cl; last_pop:=true; {final |pop|}
  2963.     end;
  2964.   end;
  2965. done:if (vf_ptr<>0)or(vf_loc<>vf_limit) then bad_vf
  2966. @ For a |push| we either increase |vf_push_num| or start a new level and
  2967. append a |push|.
  2968. @d incr_stack(#)==
  2969. if #=stack_used then
  2970.   if stack_used=stack_size then overflow(str_stack,stack_size)
  2971.   else incr(stack_used);
  2972. incr(#)
  2973. @<VF: Do a |push|@>=
  2974. if (vf_ptr>0)and(vf_push_loc[vf_ptr]=byte_ptr) then
  2975.   begin if vf_push_num[vf_ptr]=255 then overflow(str_stack,255);
  2976.   incr(vf_push_num[vf_ptr]);
  2977.   end
  2978. else  begin incr_stack(vf_ptr);
  2979.   @<VF: Start a new level@>;
  2980.   vf_push_num[vf_ptr]:=0;
  2981.   end
  2982. @ @<VF: Start a new level@>=
  2983. append_one(push);
  2984. vf_move[vf_ptr]:=vf_move[vf_ptr-1];
  2985. vf_push_loc[vf_ptr]:=byte_ptr;
  2986. vf_last_end[vf_ptr]:=byte_ptr;
  2987. vf_last[vf_ptr]:=vf_other
  2988. @ When a character, a rule, or an |xxx| is appended, transformation
  2989. rule~1 might be applicable.
  2990. @<VF: Do a |char|, |rule|, or |xxx|@>=
  2991. begin if (vf_ptr=0)or(byte_ptr>vf_push_loc[vf_ptr]) then move_zero:=false
  2992. else case cur_class of
  2993. char_cl: move_zero:=(not cur_upd)or(cur_fnt<>vf_fnt);
  2994. rule_cl: move_zero:=not cur_upd;
  2995. xxx_cl: move_zero:=true;
  2996. end; {there are no other cases}
  2997. if move_zero then  begin decr(byte_ptr); decr(vf_ptr);
  2998.   end;
  2999. case cur_class of
  3000. char_cl: @<VF: Do a |fnt|, a |char|, or both@>;
  3001. rule_cl: @<VF: Do a |rule|@>;
  3002. xxx_cl: @<VF: Do an |xxx|@>;
  3003. end; {there are no other cases}
  3004. vf_last_end[vf_ptr]:=byte_ptr;
  3005. if move_zero then
  3006.   begin incr(vf_ptr); append_one(push); vf_push_loc[vf_ptr]:=byte_ptr;
  3007.   vf_last_end[vf_ptr]:=byte_ptr;
  3008.   if cur_class=char_cl then if cur_upd then goto reswitch;
  3009.   end;
  3010. @ A special situation arises if transformation rule~1 is applied to a
  3011. |fnt_num| of |fnt| command, but not to the |set_char| or \\{set} command
  3012. following it; in this case |cur_upd| and |move_zero| are both |true| and
  3013. the |set_char| or \\{set} command will be appended later.
  3014. @<VF: Do a |fnt|, a |char|, or both@>=
  3015. begin if cur_fnt<>vf_fnt then
  3016.   begin vf_last[vf_ptr]:=vf_other;
  3017.   pckt_unsigned(fnt1,cur_fnt); vf_fnt:=cur_fnt;
  3018.   end;
  3019. if (not move_zero)or(not cur_upd) then
  3020.   begin vf_last[vf_ptr]:=vf_char_type[cur_upd];
  3021.   vf_last_loc[vf_ptr]:=byte_ptr;
  3022.   pckt_char(cur_upd,cur_ext,cur_res);
  3023.   end;
  3024. @ @<VF: Do a |rule|@>=
  3025. begin vf_last[vf_ptr]:=vf_rule_type[cur_upd];
  3026. vf_last_loc[vf_ptr]:=byte_ptr;
  3027. append_one(dvi_rule_cmd[cur_upd]);
  3028. pckt_four(cur_v_dimen); pckt_four(cur_h_dimen);
  3029. @ @<VF: Do an |xxx|@>=
  3030. begin vf_last[vf_ptr]:=vf_other;
  3031. pckt_unsigned(xxx1,cur_parm); pckt_room(cur_parm);
  3032. while cur_parm>0 do
  3033.   begin append_byte(vf_ubyte); decr(cur_parm);
  3034.   end;
  3035. @ Transformation rules 2--6 are triggered by a |pop|, either read from
  3036. the \.{VF} file or manufactured at the end of the packet.
  3037. @<VF: Do a |pop|@>=
  3038. begin if vf_ptr<1 then bad_vf;
  3039. byte_ptr:=vf_last_end[vf_ptr]; {this is rule 2}
  3040. if vf_last[vf_ptr]<=vf_rule then
  3041.  if vf_last_loc[vf_ptr]=vf_push_loc[vf_ptr] then
  3042.   @<VF: Prepare for rule 4@>;
  3043. if byte_ptr=vf_push_loc[vf_ptr] then @<VF: Apply rule 3 or 4@>
  3044. else  begin if vf_last[vf_ptr]=vf_group then @<VF: Apply rule 6@>;
  3045.   append_one(pop); decr(vf_ptr); vf_last[vf_ptr]:=vf_group;
  3046.   vf_last_loc[vf_ptr]:=vf_push_loc[vf_ptr+1]-1;
  3047.   vf_last_end[vf_ptr]:=byte_ptr;
  3048.   if vf_push_num[vf_ptr+1]>0 then @<VF: Apply rule 5@>;
  3049.   end;
  3050. @ In order to implement transformation rule~4, we cancel the |set_char|,
  3051. \\{set}, or |set_rule|, append a |pop|, and insert a |put| or |put_rule|
  3052. with the old parameters.
  3053. @<VF: Prepare for rule 4@>=
  3054. begin cur_class:=vf_last[vf_ptr]; cur_upd:=false;
  3055. byte_ptr:=vf_push_loc[vf_ptr];
  3056. @ @<VF: Apply rule 3 or 4@>=
  3057. begin if vf_push_num[vf_ptr]>0 then
  3058.   begin decr(vf_push_num[vf_ptr]);
  3059.   vf_move[vf_ptr]:=vf_move[vf_ptr-1];
  3060.   end
  3061. else  begin decr(byte_ptr); decr(vf_ptr);
  3062.   end;
  3063. if cur_class<>pop_cl then goto reswitch; {this is rule 4}
  3064. @ @<VF: Apply rule 6@>=
  3065. begin Decr(byte_ptr)(2);
  3066. for k:=vf_last_loc[vf_ptr]+1 to byte_ptr do byte_mem[k-1]:=byte_mem[k];
  3067. vf_last[vf_ptr]:=vf_other; vf_last_end[vf_ptr]:=byte_ptr;
  3068. @ @<VF: Apply rule 5@>=
  3069. begin incr(vf_ptr);
  3070. @<VF: Start a new level@>;
  3071. decr(vf_push_num[vf_ptr]);
  3072. @ Finally a type is assigned to the packet just built: |vf_simple| if
  3073. the packet ends with a character of the correct width, or |vf_complex|
  3074. in all other cases; if a |vf_simple| packet for a character with
  3075. extension zero consists of just one character with extension zero and
  3076. the same residue, and if there is no previous packet, the whole packet
  3077. is replaced by the empty packet.
  3078. @d vf_simple=0 {the packet ends with a character of the correct width}
  3079. @d vf_complex=1 {otherwise}
  3080. @<VF: Build the final form of the character packet@>=
  3081. temp_byte:=vf_complex; {just in case}
  3082. if vf_last[0]=vf_put then if cur_wp=cur_vf_wp then temp_byte:=vf_simple;
  3083. k:=pckt_start[pckt_ptr]; Incr(byte_mem[k])(temp_byte);
  3084. if (bo(byte_mem[k])=0)and@|(vf_push_loc[0]=vf_last_loc[0])and@|
  3085.   (cur_ext=0)and@|(cur_res=cur_vf_res) then byte_ptr:=k;
  3086. font_vf_packet(cur_vf)(cur_vf_res):=make_packet
  3087. @ The \.{VF} format specifies that after a character packet invoked by a
  3088. |set_char| or \\{set} command, ``|h|~is increased by the \.{TFM} width
  3089. (properly scaled)---just as if a simple character had been typeset'';
  3090. for |vf_simple| packets this is achieved by changing the final |put|
  3091. command into |set_char| or \\{set}, but for |vf_complex| packets an
  3092. explicit movement must be done. This poses a problem for programs,
  3093. such as \.{DVIcopy}, which write a new \.{DVI} file with all references
  3094. to characters from virtual fonts replaced by their character packets:
  3095. The \.{DVItype} program specifies that the horizontal movements after a
  3096. |set_char| or \\{set} command, after a |set_rule| command, and after one
  3097. of the commands |right1..x4|, are all treated differently when \.{DVI}
  3098. units are converted to pixels.
  3099. Thus we introduce a slight extension of \.{DVItype}'s pixel rounding
  3100. algorithm and hope that this extension will become part of the standard
  3101. \.{DVItype} program in the near future: If a \.{DVI} file contains a
  3102. |set_rule| command for a rule with the negative height |width_dimen|,
  3103. then this rule shall be treated in exactly the same way as a ficticious
  3104. character whose width is the width of that rule; as value of |width_dimen|
  3105. we choose $-2^{31}$, the smallest signed 32-bit integer.
  3106. @<Glob...@>=
  3107. @!width_dimen:int_32; {vertical dimension of special rules}
  3108. @ When initializing |width_dimen| we are careful to avoid arithmetic
  3109. overflow.
  3110. @<Set init...@>=
  3111. width_dimen:=-@"40000000; Decr(width_dimen)(@"40000000);
  3112. @ If no font directory has been specified, \.{\title} is supposed to use
  3113. the default \.{VF} directory, which is a system-dependent place where
  3114. the \.{VF} files for standard fonts are kept.
  3115. The string variable |VF_default_area| contains the name of this area.
  3116. @^system dependencies@>
  3117. @d VF_default_area_name=='TeXvfonts:' {change this to the correct name}
  3118. @d VF_default_area_name_length=10 {change this to the correct length}
  3119. @<Glob...@>=
  3120. @!VF_default_area:packed array[1..VF_default_area_name_length] of char;
  3121. @ @<Set init...@>=
  3122. VF_default_area:=VF_default_area_name;
  3123. @ The function |do_vf| attempts to read the \.{VF} file for a font and
  3124. returns |false| if the \.{VF} file could not be found; when the \.{VF}
  3125. file has been read, the font type is changed to |vf_font_type|.
  3126. @p function do_vf:boolean; {read a \.{VF} file}
  3127. label not_found,exit;
  3128. var temp_byte:int_8u; {byte for temporary variables}
  3129. @!k:int_15; {general purpose variable}
  3130. @!save_ext:int_24; {used to save |cur_ext|}
  3131. @!save_res:int_8u; {used to save |cur_res|}
  3132. @!save_wp:width_pointer; {used to save |cur_wp|}
  3133. @!save_upd:boolean; {used to save |cur_upd|}
  3134. begin save_ext:=cur_ext; save_res:=cur_res; save_wp:=cur_wp;
  3135. save_upd:=cur_upd; cur_vf:=cur_fnt; {save}
  3136. for k:=1 to VF_default_area_name_length do
  3137.   cur_name[k]:=VF_default_area[k];
  3138. make_name(font_name(cur_vf),vf_ext,VF_default_area_name_length);@/
  3139. @<VF: Open |vf_file| or |goto not_found|@>;
  3140. font_type(cur_vf):=vf_font_type;@/
  3141. @<VF: Process the preamble@>;@/
  3142. @<VF: Process the font definitions@>;@/
  3143. @<VF: Process the character packets@>;@/
  3144. @!debug print('VF file for font ',cur_vf:1); print_font(cur_vf);
  3145. print_ln(' loaded.');
  3146. gubed @;@/
  3147. @<VF: Close |vf_file|@>;@/
  3148. cur_ext:=save_ext; cur_res:=save_res; cur_wp:=save_wp;
  3149. cur_upd:=save_upd; cur_fnt:=cur_vf; {restore}
  3150. do_vf:=true; return;
  3151. not_found:do_vf:=false;
  3152. exit:end;
  3153. @ @<VF: Process the preamble@>=
  3154. if vf_ubyte<>pre then bad_vf;
  3155. if vf_ubyte<>vf_id then bad_vf;
  3156. temp_byte:=vf_ubyte; pckt_room(temp_byte);
  3157. for k:=1 to temp_byte do append_byte(vf_ubyte);
  3158. print('VF file: '''); print_packet(new_packet); print_ln(''',');
  3159. flush_packet;
  3160. font_check(nf):=vf_squad;
  3161. check_check_sum(nf,font_check(cur_vf));
  3162. font_design(nf):=round(tfm_conv*vf_pquad);
  3163. check_design_size(nf,font_design(cur_vf));
  3164. print('        for font ',cur_vf:1); print_font(cur_vf); print_ln('.')
  3165. @ @<VF: Process the font definitions@>=
  3166. z:=font_scaled(cur_vf);
  3167. @<Replace |z|...@>;@/
  3168. vf_i_fnts[0]:=invalid_font; vf_nf:=0;@/
  3169. cur_cmd:=vf_ubyte;
  3170. while (cur_cmd>=fnt_def1)and(cur_cmd<=fnt_def1+3) do
  3171.   begin case cur_cmd-fnt_def1 of
  3172.   0: cur_parm:=vf_ubyte;
  3173.   1: cur_parm:=vf_upair;
  3174.   2: cur_parm:=vf_utrio;
  3175.   3: cur_parm:=vf_squad;
  3176.   end; {there are no other cases}
  3177.   vf_do_font;
  3178.   cur_cmd:=vf_ubyte;
  3179.   end;
  3180. font_vf_fnt(cur_vf):=vf_i_fnts[0]
  3181. @ @<VF: Process the character packets@>=
  3182. font_vf_chars(cur_vf):=make_char_packets(cur_vf); {allocate packets}
  3183. while cur_cmd<=long_char do
  3184.   begin vf_do_char;
  3185.   cur_cmd:=vf_ubyte;
  3186.   end;
  3187. if cur_cmd<>post then bad_vf
  3188. @* Low-level output routines.
  3189. The program uses the binary file variable |out_file| for its main output
  3190. file; |out_loc| is the number of the byte about to be written next on
  3191. |out_file|.
  3192. @<Glob...@>=
  3193. @!out_file:byte_file; {the \.{DVI} file we are writing}
  3194. @!out_loc:int_32; {where we are about to write, in |out_file|}
  3195. @!out_back:int_32; {a back pointer}
  3196. @!out_max_v:int_31; {maximum |v| value so far}
  3197. @!out_max_h:int_31; {maximum |h| value so far}
  3198. @!out_stack:int_16u; {maximum stack depth}
  3199. @!out_pages:int_16u; {total number of pages}
  3200. @ @<Set ini...@>=
  3201. out_loc:=0; out_back:=-1;
  3202. out_max_v:=0; out_max_h:=0;
  3203. out_stack:=0; out_pages:=0;
  3204. @ To prepare |out_file| for output, we |rewrite| it.
  3205. @<Open output file(s)@>=
  3206. rewrite(out_file); {prepares to write packed bytes to |out_file|}
  3207. @ For some operating systems it may be necessary to close |out_file|.
  3208. @<Close output file(s)@>=
  3209. @ Writing the |out_file| should be done as efficient as possible for a
  3210. particular system; on many systems this means that a large number of
  3211. bytes will be accumulated in a buffer and is then written from that
  3212. buffer to |out_file|. In order to simplify such system dependent changes
  3213. we use the \.{WEB} macro |out_byte| to write the next \.{DVI} byte. Here
  3214. we give a simple minded definition for this macro in terms of standard
  3215. \PASCAL.
  3216. @^system dependencies@>
  3217. @^optimization@>
  3218. @d out_byte(#) == write(out_file,#) {write next \.{DVI} byte}
  3219. @ The \.{WEB} macro |out_one| is used to write one byte and to update
  3220. |out_loc|.
  3221. @d out_one(#) == begin out_byte(#); incr(out_loc); @+ end
  3222. @ First the |out_packet| procedure copies a packet to |out_file|.
  3223. @p procedure out_packet(@!p:pckt_pointer);
  3224. var k:byte_pointer; {index into |byte_mem|}
  3225. begin Incr(out_loc)(pckt_length(p));
  3226. for k:=pckt_start[p] to pckt_start[p+1]-1 do out_byte(bo(byte_mem[k]));
  3227. @ Next are the procedures used to write integer numbers or even complete
  3228. \.{DVI} commands to |out_file|; they all keep |out_loc| up to date.
  3229. The |out_four| procedure outputs four bytes in two's complement notation,
  3230. without risking arithmetic overflow.
  3231. @p procedure out_four(@!x:int_32); {output four bytes}
  3232. @!begin_four; comp_four(out_byte); Incr(out_loc)(4);
  3233. @ The |out_char| procedure outputs a |set_char| or \\{set} command or, if
  3234. |upd=false|, a |put| command.
  3235. @p procedure out_char(@!upd:boolean;@!ext:int_32;@!res:eight_bits);
  3236.   {output \\{set} or |put|}
  3237. @!begin_char; comp_char(out_one);
  3238. @ The |out_unsigned| procedure outputs a |fnt|, |xxx|, or |fnt_def|
  3239. command with its first parameter (normally unsigned); a |fnt| command
  3240. is converted into |fnt_num| whenever this is possible.
  3241. @p procedure out_unsigned(@!o:eight_bits;@!x:int_32);
  3242.   {output |fnt_num|, |fnt|, |xxx|, or |fnt_def|}
  3243. @!begin_unsigned; comp_unsigned(out_one);
  3244. @ The |out_signed| procedure outputs a movement (|right|, |w|,
  3245. |x|, |down|, |y|, or |z|) command with its (signed) parameter.
  3246. @p procedure out_signed(@!o:eight_bits;@!x:int_32);
  3247.   {output |right|, |w|, |x|, |down|, |y|, or |z|}
  3248. @!begin_signed; comp_signed(out_one);
  3249. @ Here we define the additional |font_data| fields required for the real
  3250. fonts used in |out_file|.
  3251. In order to simplify the \.{web2c} translation the fields in the variant
  3252. for |f_type=out_font_type| are accessed through the \.{WEB} macro
  3253. |out_font_data|.
  3254. @^font types@>@.web2c@>
  3255. @d out_font_data(#)==font_data[#] {access |out_font_type| variant fields}
  3256. @d font_out(#)==out_font_data(#).out_field {font number in |out_file|}
  3257. @<Cases for |font_record|@>=
  3258. out_font_type:
  3259. (@!out_field:font_number); {font number in |out_file|}
  3260. @ The global variable |out_nf| is the number of fonts already used in
  3261. |out_file| and the array |out_fnts| contains their internal font numbers;
  3262. the current font in |out_file| is called |out_fnt|.
  3263. @<Glob...@>=
  3264. @!out_fnts:array[font_number] of font_number; {internal font numbers}
  3265. @!out_nf:font_number; {number of fonts used in |out_file|}
  3266. @!out_fnt:font_number; {internal font number of current output font}
  3267. @ @<Set init...@>=
  3268. out_nf:=0;
  3269. @ @<Print more font usage statistics@>=
  3270. print(out_nf:1,' out, ');
  3271. @ The |out_fnt_def| procedure outputs a complete font definition
  3272. command.
  3273. @p procedure out_fnt_def(@!f:font_number);
  3274. var p:pckt_pointer; {the font name packet}
  3275. @!k,@!l:byte_pointer; {indices into |byte_mem|}
  3276. @!a:eight_bits; {length of area part}
  3277. begin out_unsigned(fnt_def1,font_out(f)); out_four(font_check(f));
  3278. out_four(font_scaled(f)); out_four(font_design(f));@/
  3279. p:=font_name(f); k:=pckt_start[p]; l:=pckt_start[p+1]-1;
  3280. a:=bo(byte_mem[k]);@/
  3281. Incr(out_loc)(l-k+2); out_byte(a); out_byte(l-k-a);
  3282. while k<l do
  3283.   begin incr(k); out_byte(bo(byte_mem[k]));
  3284.   end;
  3285. @* Writing the output file.
  3286. Here we define the device dependent parts of the typesetting routines
  3287. described later in this program.
  3288. The device dependent code for a real output device must define a few constants;
  3289. here we demonstrate how they should be defined.
  3290. @d h_resolution=300 {horizontal resolution in pixels per inch (dpi)}
  3291. @d v_resolution=300 {vertical resolution in pixels per inch (dpi)}
  3292. @ These are the local variables (if any) needed for |do_pre|.
  3293. @<OUT: Local variables for |do_pre|@>=
  3294. var k:int_15; {general purpose variable}
  3295. @!p,@!q,@!r:byte_pointer; {indices into |byte_mem|}
  3296. @!comment:packed array[1..comm_length] of char; {preamble comment prefix}
  3297. @ And here is the device dependent code for |do_pre|; the \.{DVI} preamble
  3298. comment written to |out_file| is similar to the one produced by \.{GFtoPK},
  3299. but we want to prepend our own preamble comment string only once.
  3300. @<OUT: Process the |pre|@>=
  3301. out_one(pre); out_one(dvi_id);
  3302. out_four(dvi_num); out_four(dvi_den); out_four(dvi_mag);@/
  3303. p:=pckt_start[pckt_ptr-1]; q:=byte_ptr; {location of old \.{DVI} comment}
  3304. comment:=preamble_comment; pckt_room(comm_length);
  3305. for k:=1 to comm_length do append_byte(xord[comment[k]]);
  3306. while byte_mem[p]=bi(" ") do incr(p); {remove leading blanks}
  3307. if p=q then Decr(byte_ptr)(from_length)
  3308. else begin k:=0;
  3309.   while (k<comm_length)and(byte_mem[p+k]=byte_mem[q+k]) do incr(k);
  3310.   if k=comm_length then Incr(p)(comm_length);
  3311.   end;
  3312. k:=byte_ptr-p; {total length}
  3313. if k>255 then
  3314.   begin k:=255; q:=p+255-comm_length; {at most 255 bytes}
  3315.   end;
  3316. out_one(k); out_packet(new_packet); flush_packet;
  3317. for r:=p to q-1 do out_one(bo(byte_mem[r]));
  3318. @ These are the additional local variables (if any) needed for |do_bop|;
  3319. the variables |@!i| and |@!j| are already declared.
  3320. @<OUT: More local variables for |do_bop|@>=
  3321. @ And here is the device dependent code for |do_bop|.
  3322. @<OUT: Process a |bop|@>=
  3323. out_one(bop); incr(out_pages);
  3324. for i:=0 to 9 do out_four(count[i]);
  3325. out_four(out_back); out_back:=out_loc-45;
  3326. out_fnt:=invalid_font;
  3327. @ These are the local variables (if any) needed for |do_eop|.
  3328. @<OUT: Local variables for |do_eop|@>=
  3329. @{var@}
  3330. @ And here is the device dependent code for |do_eop|.
  3331. @<OUT: Process an |eop|@>=
  3332. out_one(eop);
  3333. @ These are the local variables (if any) needed for |do_push|.
  3334. @<OUT: Local variables for |do_push|@>=
  3335. @{var@}
  3336. @ And here is the device dependent code for |do_push|.
  3337. @<OUT: Process a |push|@>=
  3338. if stack_ptr>out_stack then out_stack:=stack_ptr;
  3339. out_one(push);
  3340. @ These are the local variables (if any) needed for |do_pop|.
  3341. @<OUT: Local variables for |do_pop|@>=
  3342. @{var@}
  3343. @ And here is the device dependent code for |do_pop|.
  3344. @<OUT: Process a |pop|@>=
  3345. out_one(pop);
  3346. @ These are the additional local variables (if any) needed for |do_xxx|;
  3347. the variable |@!p|, the pointer to the packet containing the special
  3348. string, is already declared.
  3349. @<OUT: More local variables for |do_xxx|@>=
  3350. @ And here is the device dependent code for |do_xxx|.
  3351. @<OUT: Process an |xxx|@>=
  3352. out_unsigned(xxx1,pckt_length(p)); out_packet(p);
  3353. @ These are the local variables (if any) needed for |do_right|.
  3354. @<OUT: Local variables for |do_right|@>=
  3355. @{var@}
  3356. @ And here is the device dependent code for |do_right|.
  3357. @<OUT: Process a |right| or |w| or |x|@>=
  3358. if cur_class<right_cl then out_one(cur_cmd) {|w0| or |x0|}
  3359. else out_signed(dvi_right_cmd[cur_class],cur_parm); {|right|, |w|, or |x|}
  3360. @ Here we update the |out_max_h| value.
  3361. @<OUT: Move right@>=
  3362. if abs(cur_h)>out_max_h then out_max_h:=abs(cur_h);
  3363. @ These are the local variables (if any) needed for |do_down|.
  3364. @<OUT: Local variables for |do_down|@>=
  3365. @{var@}
  3366. @ And here is the device dependent code for |do_down|.
  3367. @<OUT: Process a |down| or |y| or |z|@>=
  3368. if cur_class<down_cl then out_one(cur_cmd) {|y0| or |z0|}
  3369. else out_signed(dvi_down_cmd[cur_class],cur_parm); {|down|, |y|, or |z|}
  3370. @ Here we update the |out_max_v| value.
  3371. @<OUT: Move down@>=
  3372. if abs(cur_v)>out_max_v then out_max_v:=abs(cur_v);
  3373. @ These are the local variables (if any) needed for |do_width|.
  3374. @<OUT: Local variables for |do_width|@>=
  3375. @{var@}
  3376. @ And here is the device dependent code for |do_width|.
  3377. @<OUT: Typeset a |width|@>=
  3378. out_one(set_rule);
  3379. out_four(width_dimen); out_four(cur_h_dimen);
  3380. @ These are the additional local variables (if any) needed for |do_rule|;
  3381. the variable |@!visible| is already declared.
  3382. @<OUT: More local variables for |do_rule|@>=
  3383. @ And here is the device dependent code for |do_rule|.
  3384. @<OUT: Typeset a visible |rule|@>=
  3385. out_one(dvi_rule_cmd[cur_upd]);
  3386. out_four(cur_v_dimen); out_four(cur_h_dimen);
  3387. @ @<OUT: Typeset an invisible |rule|@>=
  3388. @<OUT: Typeset a visible |rule|@>
  3389. @ These are the local variables (if any) needed for |do_font|.
  3390. @<OUT: Local variables for |do_font|@>=
  3391. @{var@}
  3392. @ And here is the device dependent code for |do_font|; if the \.{VF} file
  3393. for a file could not be found, we simply assume this must be a real font.
  3394. @<OUT: Look for a font file before trying to read the \.{VF} file;
  3395.   if found |goto done|@>=
  3396. @ @<OUT: Look for a font file after trying to read the \.{VF} file;
  3397.   if found |goto done|@>=
  3398. if(out_nf>=max_fonts) then overflow(str_fonts,max_fonts);
  3399. print('OUT: font ',cur_fnt:1); d_print(' => ',out_nf:1);
  3400. print_font(cur_fnt);
  3401. d_print(' at ',font_scaled(cur_fnt):1,' DVI units'); print_ln('.');
  3402. font_type(cur_fnt):=out_font_type; font_out(cur_fnt):=out_nf;
  3403. out_fnts[out_nf]:=cur_fnt; incr(out_nf);
  3404. out_fnt_def(cur_fnt); goto done;
  3405. @ These are the local variables (if any) needed for |do_char|.
  3406. @<OUT: Local variables for |do_char|@>=
  3407. @{var@}
  3408. @ And here is the device dependent code for |do_char|.
  3409. @<OUT: Typeset a |char|@>=
  3410. begin @!debug if font_type(cur_fnt)<>out_font_type then confusion(str_fonts);
  3411. gubed @;
  3412. if cur_fnt<>out_fnt then
  3413.   begin out_unsigned(fnt1,font_out(cur_fnt)); out_fnt:=cur_fnt;
  3414.   end;
  3415. out_char(cur_upd,cur_ext,cur_res);
  3416. @ If the program terminates in the middle of a page, we write as many
  3417. |pop|s as necessary and one |eop|.
  3418. @<OUT: Finish incomplete page@>=
  3419. begin while stack_ptr>0 do
  3420.   begin out_one(pop); decr(stack_ptr);
  3421.   end;
  3422.   out_one(eop);
  3423. @ If the output file has been started, we write the postamble; in
  3424. addition we print the number of bytes and pages written to |out_file|.
  3425. @<OUT: Finish output file(s)@>=
  3426. if out_loc>0 then
  3427.   begin @<OUT: Write the postamble@>;
  3428.   k:=7-((out_loc-1) mod 4); {the number of 223's}
  3429.   while k>0 do
  3430.     begin out_one(223); decr(k);
  3431.     end;
  3432.   print('OUT file: ',out_loc:1,' bytes, ',out_pages:1,' page');
  3433.   if out_pages<>1 then print('s');
  3434.   end
  3435. else print('OUT file: no output');
  3436. print_ln(' written.');
  3437. if out_pages=0 then mark_harmless;
  3438. @ Here we simply write the values accumulated during the \.{DVI} output.
  3439. @<OUT: Write the postamble@>=
  3440. out_one(post); out_four(out_back); out_back:=out_loc-5;@/
  3441. out_four(dvi_num); out_four(dvi_den); out_four(dvi_mag);@/
  3442. out_four(out_max_v); out_four(out_max_h);@/
  3443. out_one(out_stack div @"100); out_one(out_stack mod @"100);@/
  3444. out_one(out_pages div @"100); out_one(out_pages mod @"100);@/
  3445. k:=out_nf;
  3446. while k>0 do
  3447.   begin decr(k); out_fnt_def(out_fnts[k]);
  3448.   end;
  3449. out_one(post_post); out_four(out_back);@/
  3450. out_one(dvi_id)
  3451. @ Here we could print more memory usage statistics; this possibility is,
  3452. however, not used for \.{DVIcopy}.
  3453. @<Print more memory usage statistics@>=
  3454. @* Subroutines for typesetting commands.
  3455. This is the central part of the whole \.{\title} program:
  3456. When a typesetting command from the \.{DVI} file or from a \.{VF} packet
  3457. has been decoded, one of the typesetting routines defined below is
  3458. invoked to execute the command; apart from the necessary book keeping,
  3459. these routines invoke device dependent code defined earlier.
  3460. These typesetting routines communicate with the rest of the program
  3461. through global variables.
  3462. @<Glob...@>=
  3463. @!type_setting:boolean; {|true| while typesetting a page}
  3464. @!count:array[0..9] of int_32; {counts from last |bop| command}
  3465. @!device
  3466. @!h_conv:real; {converts \.{DVI} units to horizontal pixels}
  3467. @!v_conv:real; {converts \.{DVI} units to vertical pixels}
  3468. @!h_pixels:pix_value; {a horizontal dimension in pixels}
  3469. @!v_pixels:pix_value; {a vertical dimension in pixels}
  3470. @!temp_pix:pix_value; {temporary value for pixel rounding}
  3471. ecived
  3472. @ @<Set init...@>=
  3473. type_setting:=false;
  3474. @ A stack is used to keep track of the current horizonal and vertical
  3475. position, |h| and |v|, and the four registers |w|, |x|, |y|, and |z|;
  3476. the register pairs |(w,x)| and |(y,z)| are maintained as arrays.
  3477. @<Types...@>=
  3478. @!stack_pointer=0..stack_size;@/
  3479. @!pair_32=array[0..1] of int_32; {a pair of |int_32| variables}
  3480. @!stack_record=record@;@/
  3481.   @!h_field:int_32; {horizontal position |h|}
  3482.   @!v_field:int_32; {vertical position |v|}
  3483.     @!device
  3484.     @!hh_field:pix_value; {horizontal pixel position |hh|}
  3485.     @!vv_field:pix_value; {vertical pixel position |vv|}
  3486.     ecived @; @/
  3487.   @!w_x_field:pair_32; {|w| and |x| register for horizontal movements}
  3488.   @!y_z_field:pair_32; {|y| and |z| register for vertical movements}
  3489.   end;
  3490. @ The current values are kept in |cur_stack|; they are pushed onto and
  3491. popped from |stack|. We use \.{WEB} macros to access the current values.
  3492. @d cur_h==cur_stack.h_field {the current |@!h| value}
  3493. @d cur_v==cur_stack.v_field {the current |@!v| value}
  3494. @d cur_hh==cur_stack.hh_field {the current |@!hh| value}
  3495. @d cur_vv==cur_stack.vv_field {the current |@!vv| value}
  3496. @d cur_w_x==cur_stack.w_x_field {the current |@!w| and |@!x| value}
  3497. @d cur_y_z==cur_stack.y_z_field {the current |@!y| and |@!z| value}
  3498. @<Glob...@>=
  3499. @!stack:array[1..stack_size] of stack_record; {the pushed values}
  3500. @!cur_stack:stack_record; {the current values}
  3501. @!zero_stack:stack_record; {initial values}
  3502. @!stack_ptr:stack_pointer; {last used position in |stack|}
  3503. @ @<Set init...@>=
  3504. stack_ptr:=0;
  3505. zero_stack.h_field:=0; zero_stack.v_field:=0;
  3506. @!device zero_stack.hh_field:=0; zero_stack.vv_field:=0; @+ ecived @; @/
  3507. for i:=0 to 1 do
  3508.   begin zero_stack.w_x_field[i]:=0; zero_stack.y_z_field[i]:=0;
  3509.   end;
  3510. @ A sequence of consecutive rules, or consecutive characters in a fixed-width
  3511. font whose width is not an integer number of pixels, can cause |hh| to drift
  3512. far away from a correctly rounded value. \.{\title} ensures that the
  3513. amount of drift will never exceed |max_h_drift| pixels; similarly |vv|
  3514. shall never drift away from the correctly rounded value by more than
  3515. |max_v_drift| pixels.
  3516. @d max_h_drift=2 {we insist that abs|(hh-h_pixel_round(h))<=max_drift|}
  3517. @d max_v_drift=2 {we insist that abs|(vv-v_pixel_round(v))<=max_drift|}
  3518. @ Let us start with the simple cases:
  3519. The |do_pre| procedure is called when the preamble has been read from
  3520. the \.{DVI} file; the preamble comment has just been converted into a
  3521. temporary packet with the |new_packet| procedure.
  3522. @p procedure do_pre;@/
  3523. @<OUT: Local variables for |do_pre|@>@;
  3524. begin  @!device
  3525.   h_conv:=(dvi_num/254000.0)*(h_resolution/dvi_den)*(dvi_mag/1000.0);
  3526.   v_conv:=(dvi_num/254000.0)*(v_resolution/dvi_den)*(dvi_mag/1000.0);
  3527.   ecived @; @/
  3528. @<OUT: Process the |pre|@>@;@/
  3529. @ The |do_bop| procedure is called when a |bop| has been read. This
  3530. routine determines whether a page shall be processed or skipped and sets
  3531. the variable |type_setting| accordingly.
  3532. @p procedure do_bop;
  3533. var i,@!j:0..9; {indices into |count|}
  3534. @<OUT: More local variables for |do_bop|@>@;
  3535. begin @<Determine whether this page should be processed or skipped@>;
  3536. print('DVI: ');
  3537. if type_setting then
  3538.   begin
  3539.   cur_stack:=zero_stack; cur_fnt:=invalid_font;@/
  3540.   @<OUT: Process a |bop|@>@;@/
  3541.   print('processing');
  3542.   end
  3543. else print('skipping');
  3544. print(' page ',count[0]:1); j:=9;
  3545. while (j>0)and(count[j]=0) do decr(j);
  3546. for i:=1 to j do print('.',count[i]:1);
  3547. d_print(' at ',dvi_loc-45:1);
  3548. print_ln('.');
  3549. @ For the moment a page selection mechanism is not yet implemented,
  3550. i.e., all pages are processed.
  3551. @<Determine whether this page...@>=
  3552. type_setting:=true
  3553. @ The |do_eop| procedure is called in order to process an |eop|;
  3554. the stack should be empty.
  3555. @p procedure do_eop;@/
  3556. @<OUT: Local variables for |do_eop|@>@;
  3557. begin if stack_ptr<>0 then bad_dvi;
  3558. @<OUT: Process an |eop|@>@;
  3559. type_setting:=false;
  3560. @ The procedures |do_push| and |do_pop| are called in order to process
  3561. |push| and |pop| commands; |do_push| must check for stack overflow,
  3562. |do_pop| should never be called when the stack is empty.
  3563. @p procedure do_push; {push onto stack}
  3564. @<OUT: Local variables for |do_push|@>@;
  3565. begin incr_stack(stack_ptr); stack[stack_ptr]:=cur_stack;@/
  3566. @<OUT: Process a |push|@>@;
  3567. procedure do_pop; {pop from stack}
  3568. @<OUT: Local variables for |do_pop|@>@;
  3569. begin if stack_ptr=0 then bad_dvi;
  3570. cur_stack:=stack[stack_ptr]; decr(stack_ptr);@/
  3571. @<OUT: Process a |pop|@>@;
  3572. @ The |do_xxx| procedure is called in order to process a special command;
  3573. the bytes of the special string have been put into |byte_mem| as the
  3574. current string. They are converted to a temporary packet and discarded
  3575. again.
  3576. @p procedure do_xxx;
  3577. var p:pckt_pointer; {temporary packet}
  3578. @<OUT: More local variables for |do_xxx|@>@;
  3579. begin p:=new_packet;@/
  3580. @<OUT: Process an |xxx|@>@;@/
  3581. flush_packet;
  3582. @ Next are the movement commands:
  3583. The |do_right| procedure is called in order to process the horizontal
  3584. movement commands |right|, |w|, and |x|.
  3585. @d do_h_pixels(#)== {check for proper horizontal pixel rounding}
  3586. begin Incr(cur_hh)(#); temp_pix:=h_pixel_round(cur_h);
  3587. if abs(temp_pix-cur_hh)>max_h_drift then
  3588.   if temp_pix>cur_hh then cur_hh:=temp_pix-max_h_drift
  3589.   else cur_hh:=temp_pix+max_h_drift;
  3590. @p procedure do_right;@/
  3591. @<OUT: Local variables for |do_right|@>@;
  3592. begin if cur_class>=w_cl then cur_w_x[cur_class-w_cl]:=cur_parm
  3593. else if cur_class<right_cl then cur_parm:=cur_w_x[cur_class-w0_cl];
  3594. @<OUT: Process a |right| or |w| or |x|@>@;@/
  3595. Incr(cur_h)(cur_parm);
  3596. @!device
  3597. if (cur_parm>=font_space(cur_fnt))or(cur_parm<=-4*font_space(cur_fnt)) then
  3598.  cur_hh:=h_pixel_round(cur_h)
  3599. else do_h_pixels(h_pixel_round(cur_parm));
  3600. ecived @; @/
  3601. @<OUT: Move right@>@;
  3602. @ The |do_down| procedure is called in order to process the vertical
  3603. movement commands |down|, |y|, and |z|.
  3604. @d do_v_pixels(#)== {check for proper vertical pixel rounding}
  3605. begin Incr(cur_vv)(#); temp_pix:=v_pixel_round(cur_v);
  3606. if abs(temp_pix-cur_vv)>max_v_drift then
  3607.   if temp_pix>cur_vv then cur_vv:=temp_pix-max_v_drift
  3608.   else cur_vv:=temp_pix+max_v_drift;
  3609. @p procedure do_down;
  3610. @<OUT: Local variables for |do_down|@>@;
  3611. begin if cur_class>=y_cl then cur_y_z[cur_class-y_cl]:=cur_parm
  3612. else if cur_class<down_cl then cur_parm:=cur_y_z[cur_class-y0_cl];
  3613. @<OUT: Process a |down| or |y| or |z|@>@;@/
  3614. Incr(cur_v)(cur_parm);
  3615. @!device
  3616. if abs(cur_parm)>=5*font_space(cur_fnt) then cur_vv:=v_pixel_round(cur_v)
  3617. else do_v_pixels(v_pixel_round(cur_parm));
  3618. ecived @; @/
  3619. @<OUT: Move down@>@;
  3620. @ The |do_width| procedure is called in order to increase the current
  3621. horizontal position |cur_h| by |cur_h_dimen| in exactly the same way
  3622. as if a character of width |cur_h_dimen| had been typeset.
  3623. @p procedure do_width;@/
  3624. @<OUT: Local variables for |do_width|@>@;
  3625. begin @<OUT: Typeset a |width|@>@;@/
  3626. Incr(cur_h)(cur_h_dimen);
  3627. @!device do_h_pixels(h_pixels); @+ ecived @/ @;
  3628. @<OUT: Move right@>@;
  3629. @ Finally we have the commands for the typesetting of rules and characters;
  3630. the global variable |cur_upd| is |true| if the horizontal position shall
  3631. be updated (\\{set} commands).
  3632. Here are two other subroutine that we need: They computes the number of
  3633. pixels in the height or width of a rule. Characters and rules will line up
  3634. properly if the sizes are computed precisely as specified here.  (Since
  3635. |h_conv| and |v_conv| are computed with some floating-point roundoff error,
  3636. in a machine-dependent way, format designers who are tailoring something for
  3637. a particular resolution should not plan their measurements to come out to an
  3638. exact integer number of pixels; they should compute things so that the
  3639. rule dimensions are a little less than an integer number of pixels, e.g.,
  3640. 4.99 instead of 5.00.)
  3641. @p @!device
  3642. function h_rule_pixels(x:int_32):pix_value;
  3643.   {computes $\lceil|h_conv|\cdot x\rceil$}
  3644. var n:int_32;
  3645. begin n:=trunc(h_conv*x);
  3646. if n<h_conv*x then h_rule_pixels:=n+1 @+ else h_rule_pixels:=n;
  3647. function v_rule_pixels(x:int_32):pix_value;
  3648.   {computes $\lceil|v_conv|\cdot x\rceil$}
  3649. var n:int_32;
  3650. begin n:=trunc(v_conv*x);
  3651. if n<v_conv*x then v_rule_pixels:=n+1 @+ else v_rule_pixels:=n;
  3652. ecived
  3653. @ The |do_rule| procedure is called in order to typeset a rule.
  3654. @p procedure do_rule;@/
  3655. var visible:boolean;
  3656. @<OUT: More local variables for |do_rule|@>@;
  3657. begin if (cur_h_dimen>0)and(cur_v_dimen>0) then
  3658.   begin visible:=true;
  3659.   @!device
  3660.   h_pixels:=h_rule_pixels(cur_h_dimen);
  3661.   v_pixels:=v_rule_pixels(cur_v_dimen);
  3662.   ecived @; @/
  3663.   @<OUT: Typeset a visible |rule|@>@;
  3664.   end
  3665. else  begin visible:=false;
  3666.   @<OUT: Typeset an invisible |rule|@>@;
  3667.   end;
  3668. if cur_upd then
  3669.   begin Incr(cur_h)(cur_h_dimen);
  3670.   @!device if not visible then h_pixels:=h_rule_pixels(cur_h_dimen);
  3671.   do_h_pixels(h_pixels);
  3672.   ecived @; @/
  3673.   @<OUT: Move right@>@;
  3674.   end;
  3675. @ Last not least the |do_char| procedure is called in order to typeset
  3676. character~|cur_res| with extension~|cur_ext| from the real font~|cur_fnt|.
  3677. @p procedure do_char;@/
  3678. @<OUT: Local variables for |do_char|@>@;
  3679. begin
  3680. @<OUT: Typeset a |char|@>;
  3681. if cur_upd then
  3682.   begin Incr(cur_h)(widths[cur_wp]);
  3683.   @!device do_h_pixels(font_pixel(cur_fnt)(cur_res)); @+ ecived @; @/
  3684.   @<OUT: Move right@>@;
  3685.   end;
  3686. @ If the program terminates abnormally, the following code may be
  3687. invoked in the middle of a page.
  3688. @<Finish output file(s)@>=
  3689. if type_setting then @<OUT: Finish incomplete page@>@;
  3690. @<OUT: Finish output file(s)@>
  3691. @ When the first character of font~|cur_fnt| is about to be typeset,
  3692. the |do_font| procedure is called in order to decide whether this is
  3693. a virtual font or a real font.
  3694. One step in this decision is the attempt to find and read the \.{VF}
  3695. file for this font; other attempts to locate a font file may be performed
  3696. before and after that, depending on the nature of the output device and
  3697. on the structure of the file system at a particular installation.
  3698. In any case |do_font| must change |font_type(cur_fnt)| from |new_font_type|
  3699. to anything else; as a last resort one might use the \.{TFM} width data
  3700. and leave blank spaces in the output.
  3701. @p procedure do_font;
  3702. label done;
  3703. @<OUT: Local variables for |do_font|@>@;
  3704. begin
  3705. @<OUT: Look for a font file before trying to read the \.{VF} file;
  3706.   if found |goto done|@>@;@/
  3707. if do_vf then goto done; {try to read the \.{VF} file}
  3708. @<OUT: Look for a font file after trying to read the \.{VF} file;
  3709.   if found |goto done|@>@;@/
  3710. done:
  3711. @!debug if font_type(cur_fnt)=new_font_type then confusion(str_fonts);
  3712. gubed@;
  3713. @* Interpreting VF packets.
  3714. The |pckt_first_par| procedure first reads a \.{DVI} command byte from
  3715. the packet into |cur_cmd|; then |cur_parm| is set to the value of the
  3716. first parameter (if any) and |cur_class| to the command class.
  3717. @p procedure pckt_first_par;
  3718. begin cur_cmd:=pckt_ubyte;
  3719. case dvi_par[cur_cmd] of
  3720. char_par: if cur_cmd<set1 then
  3721.   begin cur_ext:=0; cur_res:=cur_cmd; cur_upd:=true
  3722.   end
  3723. else  begin cur_upd:=(cur_cmd<put1);
  3724.   case cur_cmd-dvi_char_cmd[cur_upd] of
  3725.   0: cur_ext:=0;
  3726.   1: cur_ext:=pckt_ubyte;
  3727.   2: cur_ext:=pckt_upair;
  3728.   3: cur_ext:=pckt_strio;
  3729.   end;
  3730.   cur_res:=pckt_ubyte;
  3731.   end;
  3732. no_par: do_nothing;
  3733. dim1_par: cur_parm:=pckt_sbyte;
  3734. num1_par: cur_parm:=pckt_ubyte;
  3735. dim2_par: cur_parm:=pckt_spair;
  3736. num2_par: cur_parm:=pckt_upair;
  3737. dim3_par: cur_parm:=pckt_strio;
  3738. num3_par: cur_parm:=pckt_utrio;
  3739. three_cases(dim4_par): cur_parm:=pckt_squad; {|dim4|, |num4|, or |numu|}
  3740. rule_par:
  3741.   begin cur_v_dimen:=pckt_squad; cur_h_dimen:=pckt_squad;
  3742.   cur_upd:=(cur_cmd=set_rule);
  3743.   end;
  3744. fnt_par:cur_parm:=cur_cmd-fnt_num_0;
  3745. cur_class:=dvi_cl[cur_cmd];
  3746. @ The |do_vf_packet| procedure is called in order to interpret the
  3747. character packet for a virtual character. Such a packet may contain the
  3748. instruction to typeset a character from the same or an other virtual
  3749. virtual font; in such cases |do_vf_packet| calls itself recursively.
  3750. The recursion level, i.e., the number of times this has happened, is
  3751. kept in the global variable |n_recur| and should not exceed
  3752. |max_recursion|.
  3753. @^recursion@>
  3754. @<Types...@>=
  3755. @!recur_pointer=0..max_recursion;
  3756. @ The \.{\title} processor should detect an infinite recursion caused by
  3757. bad \.{VF} files; thus a new recursion level is entered even in cases
  3758. where this could be avoided without difficulty.
  3759. If the recursion level exceeds the allowed maximum, we want to give
  3760. a traceback how this has happened; thus some of the global variables
  3761. used in different invocations of |do_vf_packet| are saved in a stack,
  3762. others are saved as local variables of |do_vf_packet|.
  3763. @<Glob...@>=
  3764. @!recur_fnt:array[recur_pointer] of font_number; {this packet's font}
  3765. @!recur_ext:array[recur_pointer] of int_24; {this packet's extension}
  3766. @!recur_res:array[recur_pointer] of eight_bits; {this packet's residue}
  3767. @!recur_pckt:array[recur_pointer] of pckt_pointer; {the packet}
  3768. @!recur_loc:array[recur_pointer] of byte_pointer; {next byte of packet}
  3769. @!n_recur:recur_pointer; {current recursion level}
  3770. @!recur_used:recur_pointer; {highest recursion level used so far}
  3771. @ @<Set init...@>=
  3772. n_recur:=0; recur_used:=0;
  3773. @ Here now is the |do_vf_packet| procedure.
  3774. @p procedure do_vf_packet;
  3775. label continue,found,done;
  3776. var k:recur_pointer; {loop index}
  3777. @!p:pckt_pointer; {a packet}
  3778. @!f:int_8u; {packet type flag}
  3779. @!save_upd:boolean; {used to save |cur_upd|}
  3780. @!save_wp:width_pointer; {used to save |cur_wp|}
  3781. @!save_limit:byte_pointer; {used to save |cur_limit|}
  3782. begin @<VF: Save values on entry to |do_vf_packet|@>;@/
  3783. @<VF: Initialize variables for |do_vf_packet|; or |goto done|@>;@/
  3784. @<VF: Interpret the \.{DVI} commands in the packet@>;@/
  3785. done:if save_upd then
  3786.   begin cur_h_dimen:=widths[save_wp];
  3787.   @!device h_pixels:=pix_widths[save_wp]; @+ ecived @; @/
  3788.   do_width;
  3789.   end;
  3790. @<VF: Restore values on exit from |do_vf_packet|@>;@/
  3791. @ On entry to |do_vf_packet| several values must be saved.
  3792. @<VF: Save values on entry to |do_vf_packet|@>=
  3793. save_upd:=cur_upd;
  3794. save_wp:=cur_wp;@/
  3795. recur_fnt[n_recur]:=cur_fnt;
  3796. recur_ext[n_recur]:=cur_ext;
  3797. recur_res[n_recur]:=cur_res
  3798. @ Some of these values must be restored on exit from |do_vf_packet|.
  3799. @<VF: Restore values on exit from |do_vf_packet|@>=
  3800. cur_fnt:=recur_fnt[n_recur]
  3801. @ Here we initialize the variables needed to interpret a character
  3802. packet.
  3803. @<VF: Initialize variables for |do_vf_packet|; or |goto done|@>=
  3804. p:=font_vf_packet(cur_fnt)(cur_res);
  3805. if p=invalid_packet then
  3806.   begin pckt_warning; goto done;
  3807.   end;
  3808. f:=find_packet(cur_ext,p);
  3809. recur_pckt[n_recur]:=cur_pckt;
  3810. save_limit:=cur_limit;
  3811. cur_fnt:=font_vf_fnt(cur_fnt)
  3812. @ If |cur_pckt| is the empty packet, we manufacture a |put| command;
  3813. otherwise we read and interpret \.{DVI} commands from the packet.
  3814. @<VF: Interpret the \.{DVI} commands in the packet@>=
  3815. if cur_pckt=empty_packet then
  3816.   begin cur_class:=char_cl; goto found;
  3817.   end;
  3818. if cur_loc>=cur_limit then goto done;
  3819. continue: pckt_first_par;
  3820. found: case cur_class of
  3821. char_cl: @<VF: Typeset a |char|@>;
  3822. rule_cl: do_rule;
  3823. xxx_cl:
  3824.   begin pckt_room(cur_parm);
  3825.   while cur_parm>0 do
  3826.     begin append_byte(pckt_ubyte); decr(cur_parm);
  3827.     end;
  3828.   do_xxx;
  3829.   end;
  3830. push_cl: do_push;
  3831. pop_cl: do_pop;
  3832. five_cases(w0_cl): do_right; {|right|, |w|, or |x|}
  3833. five_cases(y0_cl): do_down; {|down|, |y|, or |z|}
  3834. fnt_cl: cur_fnt:=cur_parm;
  3835. othercases confusion(str_packets); {font definition or invalid}
  3836. endcases;
  3837. if cur_loc<cur_limit then goto continue
  3838. @ When a font is used for the first time, the |do_font| procedure is
  3839. called to decide whether this is a virtual font or not.
  3840. The final |put| of a simple packet may be changed into |set_char| or
  3841. \\{set}.
  3842. @<VF: Typeset a |char|@>=
  3843. begin cur_wp:=font_width(cur_fnt)(cur_res);
  3844. if font_type(cur_fnt)=new_font_type then do_font; {|cur_fnt| was not yet used}
  3845. if (cur_loc=cur_limit)and(f=vf_simple) and save_upd then
  3846.   begin save_upd:=false; cur_upd:=true;
  3847.   end;
  3848. if font_type(cur_fnt)=vf_font_type then
  3849.   @<VF: Enter a new recursion level@>
  3850. else do_char;
  3851. @ Before entering a new recursion level we must test for overflow; in
  3852. addition a few variables must be saved and restored.
  3853. A |set_char| or \\{set} followed by |pop| is changed into |put|.
  3854. @<VF: Enter a new recursion level@>=
  3855. begin recur_loc[n_recur]:=cur_loc; {save}
  3856. if cur_loc<cur_limit then
  3857.   if byte_mem[cur_loc]=bi(pop) then cur_upd:=false;
  3858. if n_recur=recur_used then
  3859.   if recur_used=max_recursion then
  3860.     @<VF: Display the recursion traceback and terminate@>
  3861.   else incr(recur_used);@/
  3862. incr(n_recur);
  3863. do_vf_packet;
  3864. decr(n_recur); {recurse}
  3865. cur_loc:=recur_loc[n_recur];
  3866. cur_limit:=save_limit; {restore}
  3867. @ @<VF: Display the recursion traceback and terminate@>=
  3868. begin print_ln(' !Infinite VF recursion?');
  3869. @.Infinite VF recursion@>
  3870. for k:=max_recursion downto 0 do
  3871.   begin print('level=',k:1,' font');
  3872.   d_print('=',recur_fnt[k]:1);
  3873.   print_font(recur_fnt[k]);
  3874.   print(' char=',recur_res[k]:1);
  3875.   if recur_ext[k]<>0 then print('.',recur_ext[k]:1);
  3876.   print_ln(' ');
  3877.   @!debug hex_packet(recur_pckt[k]); print_ln('loc=',recur_loc[k]:1);
  3878.   gubed@;
  3879.   end;
  3880. overflow(str_recursion,max_recursion);
  3881. @* Interpreting the DVI file.
  3882. When a |bop| has been read, the |do_page| procedure is called to
  3883. interpret one page of the \.{DVI} file; |do_page| returns when the
  3884. corresponding |eop| has been read.
  3885. @p procedure do_page;
  3886. label done;
  3887. var temp_byte:int_8u; {byte for temporary variables}
  3888. @!temp_int:int_32; {integer for temporary variables}
  3889. @!k:int_15; {general purpose variable}
  3890. begin for k:=0 to 9 do count[k]:=dvi_squad;
  3891. temp_int:=dvi_squad; do_bop;
  3892. dvi_first_par;
  3893. if type_setting then @<DVI: Process a page; then |goto done|@>
  3894. else @<DVI: Skip a page; then |goto done|@>;
  3895. done:if cur_cmd<>eop then bad_dvi;
  3896. if type_setting then do_eop;
  3897. @ All \.{DVI} commands are processed, as long as |cur_class<>invalid_cl|;
  3898. then we should have found an |eop|.
  3899. @<DVI: Process a page; then |goto done|@>=
  3900. loop begin
  3901.   case cur_class of
  3902.   char_cl: @<DVI: Typeset a |char|@>;
  3903.   rule_cl:
  3904.     if cur_upd and(cur_v_dimen=width_dimen) then
  3905.       begin @!device h_pixels:=h_pixel_round(cur_h_dimen); @+ ecived @; @/
  3906.       do_width;
  3907.       end
  3908.     else do_rule;
  3909.   xxx_cl:
  3910.     begin pckt_room(cur_parm);
  3911.     while cur_parm>0 do
  3912.       begin append_byte(dvi_ubyte); decr(cur_parm);
  3913.       end;
  3914.     do_xxx;
  3915.     end;
  3916.   push_cl: do_push;
  3917.   pop_cl: do_pop;
  3918.   five_cases(w0_cl): do_right; {|right|, |w|, or |x|}
  3919.   five_cases(y0_cl): do_down; {|down|, |y|, or |z|}
  3920.   fnt_cl: dvi_font;
  3921.   fnt_def_cl: dvi_do_font(random_reading);
  3922.   invalid_cl: goto done;
  3923.   end; {there are no other cases}
  3924. dvi_first_par; {get the next command}
  3925. @ While skipping a page all commands other than font definitions are
  3926. ignored.
  3927. @<DVI: Skip a page; then |goto done|@>=
  3928. loop begin
  3929.   case cur_class of
  3930.   xxx_cl: while cur_parm>0 do
  3931.     begin temp_byte:=dvi_ubyte; decr(cur_parm);
  3932.     end;
  3933.   fnt_def_cl: dvi_do_font(random_reading);
  3934.   invalid_cl: goto done;
  3935.   othercases do_nothing;
  3936.   endcases;
  3937. dvi_first_par; {get the next command}
  3938. @ When a font is used for the first time, the |do_font| procedure is
  3939. called to decide whether this is a virtual font or not.
  3940. @<DVI: Typeset a |char|@>=
  3941. begin set_cur_wp; if cur_wp=invalid_width then bad_dvi;
  3942. if font_type(cur_fnt)=new_font_type then do_font; {|cur_fnt| was not yet used}
  3943. if font_type(cur_fnt)=vf_font_type then do_vf_packet @+ else do_char;
  3944. @ The |do_dvi| procedure reads the entire \.{DVI} file and initiates
  3945. whatever actions may be necessary.
  3946. @p procedure do_dvi;
  3947. var temp_byte:int_8u; {byte for temporary variables}
  3948. @!temp_int:int_32; {integer for temporary variables}
  3949. @!k:int_15; {general purpose variable}
  3950. begin @<DVI: Process the preamble@>;
  3951. if random_reading then @<DVI: Process the postamble@>;
  3952. repeat dvi_first_par;
  3953.   while cur_class=fnt_def_cl do
  3954.     begin dvi_do_font(random_reading); dvi_first_par;
  3955.     end;
  3956.   if cur_cmd=bop then do_page;
  3957. until cur_cmd<>eop;
  3958. if cur_cmd<>post then bad_dvi;
  3959. @ @<DVI: Process the preamble@>=
  3960. if dvi_ubyte<>pre then bad_dvi;
  3961. if dvi_ubyte<>dvi_id then bad_dvi;
  3962. dvi_num:=dvi_pquad; dvi_den:=dvi_pquad; dvi_mag:=dvi_pquad;
  3963. tfm_conv:=(25400000.0/dvi_num)*(dvi_den/473628672)/16.0;
  3964. temp_byte:=dvi_ubyte; pckt_room(temp_byte);
  3965. for k:=1 to temp_byte do append_byte(dvi_ubyte);
  3966. print('DVI file: '''); print_packet(new_packet); print_ln(''',');
  3967. print_ln('         num=',dvi_num:1,', den=',dvi_den:1,', mag=',
  3968.   dvi_mag:1,'.');
  3969. do_pre; flush_packet
  3970. @ @<Glob...@>=
  3971. @!dvi_num:int_31; {numerator}
  3972. @!dvi_den:int_31; {denominator}
  3973. @!dvi_mag:int_31; {magnification}
  3974. @!dvi_pages:int_16u; {magnification}
  3975. @!dvi_back:int_32; {a back pointer}
  3976. @ @<DVI: Process the postamble@>=
  3977. begin dvi_back:=dvi_loc; {remember start of first page}
  3978. @<DVI: Find the postamble@>;
  3979. d_print_ln('DVI: postamble at ',dvi_loc-1:1);
  3980. temp_int:=dvi_pointer;
  3981. if dvi_num<>dvi_pquad then bad_dvi;
  3982. if dvi_den<>dvi_pquad then bad_dvi;
  3983. if dvi_mag<>dvi_pquad then bad_dvi;
  3984. temp_int:=dvi_squad; temp_int:=dvi_squad;
  3985. if stack_size<dvi_upair then overflow(str_stack,stack_size);
  3986. dvi_pages:=dvi_upair;
  3987. dvi_first_par;
  3988. while cur_class=fnt_def_cl do
  3989.   begin dvi_do_font(false); dvi_first_par;
  3990.   end;
  3991. if cur_cmd<>post_post then bad_dvi;
  3992. dvi_move(dvi_back); {back to start of first page}
  3993. @ @<DVI: Find the postamble@>=
  3994. temp_int:=dvi_length; if temp_int<53 then bad_dvi;
  3995. Decr(temp_int)(4);
  3996. repeat if temp_int=0 then bad_dvi;
  3997. dvi_move(temp_int); temp_byte:=dvi_ubyte; decr(temp_int);
  3998. until temp_byte<>223;
  3999. if temp_byte<>dvi_id then bad_dvi;
  4000. dvi_move(temp_int-4); if dvi_ubyte<>post_post then bad_dvi;
  4001. dvi_move(dvi_pquad); if dvi_ubyte<>post then bad_dvi
  4002. @* The main program.
  4003. The code for real devices is still rather incomplete.
  4004. Moreover several branches of the program have not been tested because
  4005. they are never used with \.{DVI} files made by \TeX\ and \.{VF} files
  4006. made by \.{VPtoVF}.
  4007. @ At the end of the program the output file(s) have to be finished and
  4008. on some systems it may be necessary to close input and\slash or output
  4009. files.
  4010. @^system dependencies@>
  4011. @p procedure close_files_and_terminate;
  4012. var k:@!int_15; {general purpose index}
  4013. begin @<Close input file(s)@>@;
  4014. @<Finish output file(s)@>@;
  4015. stat @<Print memory usage statistics@>;@+tats@;@/
  4016. @<Close output file(s)@>@;
  4017. @<Print the job |history|@>;
  4018. @ Now we are ready to put it all together.
  4019. Here is where \.{\title} starts, and where it ends.
  4020. @^system dependencies@>
  4021. @p begin initialize; {get all variables initialized}
  4022. @<Initialize predefined strings@>@;
  4023. @<Open input file(s)@>@;
  4024. @<Open output file(s)@>@;
  4025. do_dvi; {process the entire \.{DVI} file}
  4026. close_files_and_terminate;
  4027. final_end:end.
  4028. @ @<Print memory usage statistics@>=
  4029. print_ln('Memory usage statistics:');
  4030. print(dvi_nf:1,' dvi, ',lcl_nf:1,' local, ');
  4031. @<Print more font usage statistics@>@;@/
  4032. print_ln('and ',nf:1,' internal fonts of ',max_fonts:1);
  4033. print_ln(n_widths:1,' widths of ',max_widths:1,' and ',
  4034.   n_packets:1,' char packets for ',
  4035.   n_chars:1,' characters of ',max_chars:1);
  4036. print_ln(pckt_ptr:1,' byte packets of ',max_packets:1,' with ',
  4037.   byte_ptr:1,' bytes of ',max_bytes:1);
  4038. @<Print more memory usage statistics@>@;@/
  4039. print_ln(stack_used:1,' of ',stack_size:1,' stack and ',
  4040.   recur_used:1,' of ',max_recursion:1,' recursion levels.');
  4041. @ Some implementations may wish to pass the |history| value to the
  4042. operating system so that it can be used to govern whether or not other
  4043. programs are started. Here we simply report the history to the user.
  4044. @^system dependencies@>
  4045. @<Print the job |history|@>=
  4046. case history of
  4047. spotless: print_ln('(No errors were found.)');
  4048. harmless_message: print_ln('(Did you see the warning message above?)');
  4049. error_message: print_ln('(Pardon me, but I think I spotted something wrong.)');
  4050. fatal_message: print_ln('(That was a fatal error, my friend.)');
  4051. end {there are no other cases}
  4052. @* System-dependent changes.
  4053. This section should be replaced, if necessary, by changes to the program
  4054. that are necessary to make \.{DVIcopy} work at a particular installation.
  4055. It is usually best to design your change file so that all changes to
  4056. previous sections preserve the section numbering; then everybody's version
  4057. will be consistent with the printed program. More extensive changes,
  4058. which introduce new sections, can be inserted here; then only the index
  4059. itself will get a new section number.
  4060. @^system dependencies@>
  4061. @* Index.
  4062. Pointers to error messages appear here together with the section numbers
  4063. where each ident\-i\-fier is used.
  4064.